Page 1 of 1

captcha help

Posted: Mon Jan 14, 2008 2:06 am
by deeptranz03
Hi, ive got the following script that doesnt work:

Code: Select all

 
<?php
session_start();
if (isset($_POST["submit"]))
{           
    if (($_POST["captcha"]) != ($_SESSION["key"]))
    {
        die("Error: You must enter the code correctly");
    }
    else 
    {
        echo "Success!";
    }
}
?>
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="name">Name:</label></td>
    <input type="text" name="name" />      
    <label for="company">Company:</label>
    <input type="text" name="company" />
    <label for="email">Email:</label>
    <input type="text" name="email" />
    <label for="message">Message:</label>
    <textarea name="message" rows="6" cols="20"></textarea>
    <img src="captcha.php" border="0"><?php echo $_SESSION["key"]; ?>
    <input type="text" name="captcha" width="50px" />
    <input type="image" src="images/submit.png" name="submit" />  
</form>
 
 
 
here's the captcha.php referenced in the form above:
 

Code: Select all

 
<?php
 
session_start();
 
$md5 = md5(microtime() * mktime());     
$string = substr($md5, 0, 5);
             
$captcha = imagecreatefrompng("captcha.png");
 
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha, 233, 239, 239);
 
imageline($captcha, 0, 0, 39, 29, $line);
imageline($captcha, 40, 0, 64, 29, $line);
 
imagestring($captcha, 5, 20, 10, $string, $black);
$_SESSION["key"] = md5($string);
 
header("Content-type: image/png");
imagepng($captcha);
?>
basically, it doesnt seem to work, the session information generated by captcha php is different than the session info on the main file. can anyone help?

thanks

Re: captcha help

Posted: Mon Jan 14, 2008 2:15 am
by yanglei1979
Delete empty row

Re: captcha help

Posted: Mon Jan 14, 2008 2:17 am
by jimthunderbird
I guess you should change the code:

Code: Select all

if (($_POST["captcha"]) != ($_SESSION["key"]))
to

Code: Select all

if ( md5($_POST["captcha"]) != $_SESSION["key"])
?

Re: captcha help

Posted: Mon Jan 14, 2008 2:57 am
by deeptranz03
i've added the md5 function already to the if statement but it still doesnt work... i was able to make it work by using a different PHP file to handle POST data, but when I use the same PHP script (PHP_SELF) to process form data as well as captcha verification, the numbers inside the captcha image is different than the value stored in the $_SESSION["key"]

jimthunderbird wrote:I guess you should change the code:

Code: Select all

if (($_POST["captcha"]) != ($_SESSION["key"]))
to

Code: Select all

if ( md5($_POST["captcha"]) != $_SESSION["key"])
?

Re: captcha help

Posted: Mon Jan 14, 2008 6:31 am
by jimthunderbird
All right, this is interesting...

If you get rid of session_start() in captcha.php, everything will work :D
So all needs to be done, in your original script, is get rid of that single line of code at the top of captcha.php.

I learned something new...

Re: captcha help

Posted: Mon Jan 14, 2008 7:16 am
by deeptranz03
I removed the session_start() line in my original captcha.php but it still won't work... when i run my form php with the echo $_SESSION["key" code, it still shows the incorrect value for key

Re: captcha help

Posted: Mon Jan 14, 2008 10:12 am
by jimthunderbird
Then it's strange...

By removing the session_start() in captcha.php, everything works in my website.

Maybe see what other gurus would talk about...

Re: captcha help

Posted: Mon Jan 14, 2008 10:51 am
by deeptranz03
Weird indeed. Needless to say, I just placed the captcha validation on a different file that is called up when the user clicked on submit.. would have wanted the validation to happen within the same file though.. thanks anyway :)