Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP CAPTCHA and Sessions Problem

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
I am currently putting the finishing touches on my website before going fully live later. I am having an amazingly irritating issue with my CAPTCHA on the sign up form. Here is the code for the CAPTCHA:

Code:
<?php
require('includes/sessions/sessionfunc.php');
session_start();

$alphanum  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

$rand = substr(str_shuffle($alphanum), 0, 5); 

$image = imagecreatefrompng("formbg.png");
$textColor = imagecolorallocate ($image, 0, 0, 0);  
imagestring ($image, 5, 5, 8,  $rand, $textColor);  
$_SESSION['img_code'] = md5($rand); 

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);  
header("Pragma: no-cache");      
header('Content-type: image/png'); 
imagepng($image); 
imagedestroy($image); 

?>

and here is the snippet of code from the script that checks the CAPTCHA:

Code:
if($md5captcha != $_SESSION["img_code"])
			$errors[] = "The letters and numbers you entered did not match the verification image.";

where $md5captcha = md5($imgverify), where $imgverify = the POST var of the text field that contains the CAPTCHA value to match against the real one. Every time I run this script, it fails, and I get a nice red message on my test page proclaiming "The letters and numbers you entered did not match the verification image." What I would like is to have some ideas as to why this script is constantly failing.

My server details:

All GD libraries are there;
My session handling funcions are custom-made to insert session data into a MySQL database;
I have checked the session handling functions using a test script and it worked fine.

I have checked the MySQL db for the session data and it is there, I have even copied the exact (non-md5) value of the CAPTCHA and put it into the field so I know the problem cannot just be me mistyping everything. Thank you in advance!

Matt
 
try echoing out the variables you are getting in the receiver script together with the session vars.

i'm also assuming that you have started the session first...
 
Yes, the session was started. I had my script echo out the variables just like you asked and got this:

Answer: ,f3abec1f926773b14f155b0cfe5e789b

Interesting. On the left (the empty space before the comma) is where the session md5 should be. The value on the right was the md5 of what I entered.

Matt
 
The problem has been solved! I placed session_write_close after the setting the session variables and it worked perfectly. Thank you jpadie for your help!


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top