1

I want to create my custom Captcha validation in PHP and I have wrote this code but it doesn't seem to be working. The image is not being created and I cannot find where the error is.

PHP:

<?php
session_start();

$string = '';

for ($i = 0; $i < 5; $i++) {
    // this numbers refer to numbers of the ascii table (lower case)
    $string .= chr(rand(97, 122));
}

$_SESSION['rand_code'] = $string;

//specify the path where the font exists
$dir = 'fonts/arial.ttf';

$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);

imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);

header("Content-type: image/png");
imagepng($image);
?>

HTML:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p><input type="text" name="name" /> Name</p>
<p><input type="text" name="email" /> Email</p>
<p><textarea name="message"></textarea></p>
<img src="captcha.php"/>
<p><input type="text" name="code" /> Are you human?</p>
<p><input type="submit" name="submit" value="Send" class="button" /></p>
</form>
4
  • Try using PHP's imagemagick port. Commented Dec 3, 2013 at 22:28
  • This calls for basic debugging. What goes wrong exactly? Are you getting an image? If not, remove the header() call and see what error messages come up. Commented Dec 3, 2013 at 22:30
  • No i'm not getting an image. I'm getting the default browser icon for a missing image. Removing the header() neither changed anything nor shown any error messages. Commented Dec 3, 2013 at 22:42
  • Use a library. This is likely a security feature you're enabling and it's best to use a known library rather than write your own. (unless of course you're writing an improved library) Commented Dec 3, 2013 at 23:20

4 Answers 4

1

you generate a warning because of an undefined var..

change this line

imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);

to this one

imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['rand_code']);

also check, if the font file is really available and readable, or you also will get an warning

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

1 Comment

Oh God. Thank you so much. That was the case. The undefined var i was accidentally miss-typed.
1

Change this:

imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);

To this:

imagettftext ($image, 30, 0, 10, 40, $color, $dir, $_SESSION['random_code']);

You're already setting the font file name here:

$dir = 'fonts/arial.ttf';

1 Comment

Yes, i just found that out too and i changed it, but still nothing happened.
0

This is a jquery validate captcha?

1.- Do you have GD library installed?

2.- The file is named .php or .jpg? If is named .jpg, your server can execute .jpg files like a .php files?

Other recomendation. Are you trying HoneyPot validation instead of captcha?

Comments

0

first, change the text from the session var to some hardcoded string,something like

imagettftext ($image, 30, 0, 10, 40, $color, $dir , "sdfsfdsfd");

If still not working, remove the line

header("Content-type: image/png");

and open the complete URL of captcha.php in your browser. it will display some errors for sure.

1 Comment

sorry, i was writing and I forget to post it, when i did, in the mean time you already had found the answer! my bad!

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.