0

Hi i want to create a image captcha with , i have the next code for my form.

<form action="" method="POST">
    <div class="label_form">Usuario:</div> <input type="text" name="user"/><br>
    <div class="label_form">Contraseña:</div> <input type="password" name="pass"/><br>  
    <img alt="Numeros aleatorios" src="layouts/captcha.php" />  
    <input class="label_form" type="text" name="num"/><br>
    <input type="submit" value="ENTRAR" name="submit"/>
</form> 

This is the code for validation before to send the form:

if (isset($_POST["submit"])) {
  if ($_SESSION['img_number'] != $_POST['num']) {
    echo "<div class='msg_error'>Los caracteres no se corresponden.</div>";
  } else {
    /*DO STUFF*/
  }
}

And in other file with the name captcha.php i have the code for generate the image:

header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < 5; $i++) {
  $pos = rand(0, 36);
  $str. = $string {
    $pos
  };
}
$img_handle = ImageCreate(60, 22) or die("Es imposible crear la imagen");
$back_color = ImageColorAllocate($img_handle, 102, 102, 153);
$txt_color = ImageColorAllocate($img_handle, 255, 255, 255);
ImageString($img_handle, 31, 5, 0, $str, $txt_color);
Imagepng($img_handle);
session_start();
$_SESSION['img_number'] = $str;

This give a image broken showing the alt from img "Numeros aleatorios", so that tell me the file.php is calling fine the img but the code for generated is not working, any help is gratefull :D thanks.

4
  • 2
    If this is for educational purposes they this is fine, however if your wanting to use this in production I will warn you against it. Bots have become very good a reading images to crack captcha systems. I would recommend looking at googles recaptcha or search google for another captcha generator. Commented Sep 5, 2015 at 7:59
  • so you think is fine use something like this $im = imagecreatefrompng("prueba.png"); for gnerate in base from image already exist or definitely use other method? Commented Sep 5, 2015 at 8:03
  • 1
    The amount of effort to make it secure against bots would not be worth the time. I would recommend using a 3rd party like recaptcha for it. Commented Sep 5, 2015 at 8:08
  • ok yes is right,, i wiill leave the question active, because i dont understand why not working, but your suggest is usefull i will use other method to do this thanks :D Commented Sep 5, 2015 at 8:13

2 Answers 2

1

Your code is good the only thing you missed is declaring $str = "" at the top of your catpcha.php file

Here is a working capture of your code:

enter image description here

Update

Your $string length is 36, so you should generate a random position between 0 and 35, like this:

$pos = rand(0,35);

If not, you sometimes get a broken image and a PHP Notice.

Update N°2

The reason why you get a broken image without declaring $str = "" is because this line

$str .= $string{$pos};

Throws a PHP Notice and corrupts the image. (Of course only if you display_errors , that's why it worked perfectly for @Danny's server)

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing:

$str .= $string{$pos};

to

$str .= $string[$pos]; //Array of strings.

I've updated my answer:

<?php
header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789"; 
for ($i = 0; $i < 5; $i++) { 
    $pos = rand(0,36); 
    $str .= $string[$pos]; 
}
$img_handle = ImageCreate (60, 22) or die ("Es imposible crear la imagen"); 
$back_color = ImageColorAllocate($img_handle,102,102,153); 
$txt_color = ImageColorAllocate($img_handle,255,255,255); 
ImageString($img_handle, 31, 5, 0, $str, $txt_color); 
Imagepng($img_handle); 
session_start(); 
$_SESSION['img_number'] = $str;
?>

In my local server the captcha image is showing properly.

In my local server the captcha image is showing properly.

6 Comments

Is still not showing the image, only the broke img :/
Make sure about the GD Library in your PHP. Make sure about the GD Library in your PHP. I've implemented your code and it runs correctly. dfjb.webcindario.com/captcha.php
@DannyFardyJhonstonBermúdez if GD wasn't installed code would die "Es imposible crear la imagen". Check my answer to see why this works in your test server..
However, my code still works properly displaying the image on my test server.
Yes, it does indeed work. And it would work in his own server if he sets display_errors to false. But it's a good practise to solve problems, not to hide them. PS: your code will not work when the rand(0, 36) returns 36. You'll probably get just 4 characters instead of 5 in the captcha.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.