Php Captcha bug.

Discussion in 'PHP' started by Dox5, Dec 22, 2008.

  1. #1
    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:
     
    Dox5, Dec 22, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    crivion, Dec 22, 2008 IP
  3. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Yesideez, Dec 22, 2008 IP
  4. Dox5

    Dox5 Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok thanks guys i've got it now, it was the way i was testing it >.<
     
    Dox5, Dec 22, 2008 IP