Hi,
I have a captcha script which should pick up a background image and add some random letters to it and re-display
This is the part of the form that the captcha image is part of:
And here is the captcha script that is called:
I have a background image that is here:
image
As far as I can see, everything should work but I get nothing displayed at all.
Can anyone spot my errors ?
Thanks for helping.
I have a captcha script which should pick up a background image and add some random letters to it and re-display
This is the part of the form that the captcha image is part of:
Code:
<span >Verification Image:</span>
<span ><img src="captcha.php" id="captcha" />
<a href="#renew" onclick="javascript: document.getElementById('captcha').src = 'captcha.php?' + Math.random();">refresh</a> </span>
<span >Enter 8 character key:</span>
<span ><input type="text" name="imgver" id="imgver" onkeyup ="this.value=this.value.toUpperCase()"></span>
<input class="button2" type="submit" value="Give Me Free Entry Now"/>
Code:
#since we are storing our data using Sessions, we need to start a session
session_start();
#$bg_image is the image that will be used for the background of our captcha
#you will have to replace the value with your bg image.
$bg_image = "images/cap02.png";
#we're going to put some lines throughout the image to make it a bit harder for bots to crack
#to color the lines, we need to fill in the color fields using RGB values (0-255 for each color)
$line_color = array(
"R" => 150,
"G" => 150,
"B" => 150
);
#set the number of line to display in our captcha
$numLines = 5;
#set the length of the key to display in our captcha
$keyLength = 6;
#set the color of the text in our captcha
$textcolor = array(
"R" => 0,
"G" => 255,
"B" => 0
);
#get some file attribures of our bg image, all we are going to use is witdth and height.
list($width, $height, $type, $attr) = getimagesize($bg_image);
#using PHP's GD Library, we're going to create our base captcha, which starts with our BG image.
$captcha = imagecreatefrompng($bg_image);
#sets the color for our key, the color was defined above.
$keycol = imagecolorallocate($captcha, $textcolor["R"],$textcolor["G"],$textcolor["B"]);
#start a loop to add our lines to our captcha
for($i = 0; $i < $numLines; $i++)
{
$line = imagecolorallocate($captcha,$line_color["R"],$line_color["G"],$line_color["B"]);
imageline($captcha,rand(0, $width),rand(0,$height),rand(0, $width),rand(0,$height),$line);
}
#generate our random key
$string = GenKey($keyLength);
#add our random key to our captcha
imagestring($captcha, 9, rand(1, 30), rand(1, 15), $string, $keycol);
#encrypt our key and add it to our session data.
$_SESSION['key'] = md5($string);
#send HTTP header to tell client we're going to display an image.
header("Content-type: image/png");
#dsplay image
imagepng($captcha);
function GenKey ($length)
{
#define the letter / number that will be used in our key.
$chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
#start a loop to make the key.
for($i = 0; $i < $length; $i++)
{
#pick a random start place in the string
$rand_start = rand(1, strlen($chars) - 1);
#add this character to our key
$key .= substr($chars, $rand_start, 1);
}
#return our key
return $key;
}
?>
image
As far as I can see, everything should work but I get nothing displayed at all.
Can anyone spot my errors ?
Thanks for helping.
Comment