Hey I've been trying to make my own php captcha, and have it mostly working. there is just a bug that means that when the page is first loaded (starting a new session) Either no text is on the image, or there is text on the image but there is nothing in the $_SESSION. If the page is then refreshed, it works perfectly any help would be appreciated Here is my code: <?php session_start(); $Scode = substr(md5(rand(0,999)),3,5);//Creating the Security Code $width = 90;//image width $height = 30;//image Height $image = imagecreate($width,$height);//creates the image resource //setting up colours $grey = ImageColorAllocate($image, 200, 200, 200); $grey2 = ImageColorAllocate($image, 106, 106, 106); $black = ImageColorAllocate($image, 0, 0, 0); $red = ImageColorAllocate($image, 255, 50, 50); $blue = ImageColorAllocate($image, 50, 50, 255); imagefill($image,0 , 0,$grey);//filling the image background //adding in some random lines to make it harder for bots ImageRectangle($image,0,0,$width-5,$height-5,$grey2); imageline($image, 0, $height/2, $width, $height/2, $grey2); imageline($image, $width/2, 0, $width/2, $height, $grey2); imageline($image, 0, $height/4, $width, $height/4, $red); imageline($image, $width/4, 0, $width/4, $height, $red); imageline($image, 0, $height/8, $width, $height/8, $blue); imageline($image, $width/8, 0, $width/8, $height, $blue); imagestring($image,5,$width/4,$height/4,$_SESSION['code'],$black);//add the text header("Content-Type: image/jpeg");//sending header info ImageJpeg($image); //sending image ImageDestroy($image); //freeing up space $_SESSION['code'] = $Scode; //setting the Code exit(); ?> PHP:
I cant seem the error but u can check something much more simplier and well explained + working on my php tutorials blog http://www.crivionweb.com/phpblog/php-tutorial-how-to-create-a-captcha-image/ http://www.crivionweb.com/phpblog/php-tutorial-how-to-validate-the-captcha-ii/ I can answer to questions if necessary
In your IMG tag calling your captcha creator try adding something like this: <img src="imagemaker.php?i=<?=mt_rand(1,100000)?>" /> Code (markup): That effectively makes your link unique each time it is called and stops the cached version being used which might be happening in your case. The user would have to be unlucky to get the old image as it's a 1 in 100,000 chance of mt_rand() picking the same number twice. If you want I can provide you with a link to download a captcha script I made in about 10 lines. Uses TTF font, random colors, messes up the letters a bit and utilises MD5 encryption for the code.