Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: Help with Captcha Code
03-06-2007, 07:41 PM #1
- Join Date
- Jul 2005
- Thanked 0 Times in 0 Posts
Help with Captcha Code
I've got a captcha that I'm integrating into my website forms. It works but for one problem: when users forget to fill out all the required fields in the form and the php script tells them to go back to the form page and make sure to fill out all the fields - the captcha security code changes in the backend but it doesn't appear different to the users. I'd like both what they put in the field to be blank- so they can put in the new code - and I'd like the captcha code to visibily change so users know to put in a new code.
Here's the code from the form that draws in the captcha drawing:
<label for="number" id="label">Enter the text shown in the picture:</label><p><input name="number" class="text" type="text" id=\"number\"><img src="php_captcha.php" align="top"></p>
And here's the code from the captcha itself:
$RandomStr = md5(microtime());// md5 to generate the random string
$ResultStr = substr($RandomStr,0,5);//trim 5 digit
$NewImage =imagecreatefromjpeg("img.jpg");//image create by existing image and as back ground
$LineColor = imagecolorallocate($NewImage,233,239,239);//line color
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white
imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally
$_SESSION['key'] = $ResultStr;// carry the data through session
header("Content-type: image/jpeg");// out out the image
imagejpeg($NewImage);//Output image to browser
thank you for your help,
03-06-2007, 09:49 PM #2
- Join Date
- Oct 2006
- United Kingdom
- Thanked 30 Times in 29 Posts
This is due to the browser caching the image, and not loading the newly generated image. To solve this problem, the page will have to be refreshed and the source of the image must be different. You can achieve this by fooling the browser that the image is different every time.
For example, by passing a value to the captcha, which you don't use, but you pass it anyway...so that the browser thinks that it is a different image it is calling. So you will need to change the image to the following.
<img src="php_captcha.php?id=<?php echo microtime(); ?>" align="top">
Hope that resolves your problem.
03-06-2007, 09:55 PM #3
- Join Date
- Mar 2007
- Florida, USA
- Thanked 406 Times in 398 Posts
Try disabling browser caching for that image. Put this code in your captcha page:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past