Reputation: 344
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>
Upvotes: 1
Views: 3345
Reputation: 10947
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.
Upvotes: 0
Reputation: 15629
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
Upvotes: 1
Reputation: 1144
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?
Upvotes: 0
Reputation: 2475
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';
Upvotes: 1