Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with Captcha Code

    Hello,
    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=\&quot;number\&quot;><img src="php_captcha.php" align="top"></p>

    And here's the code from the captcha itself:

    <?php

    session_start();

    $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,
    sage

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    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.

    Cheers,
    Ess

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Try disabling browser caching for that image. Put this code in your captcha page:
    PHP Code:
    <?php
    header
    ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    ?>
    header()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •