captcha help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deeptranz03
Forum Newbie
Posts: 4
Joined: Mon Jan 14, 2008 1:41 am

captcha help

Post 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
Last edited by deeptranz03 on Mon Jan 14, 2008 7:10 am, edited 1 time in total.
yanglei1979
Forum Commoner
Posts: 38
Joined: Sat Aug 25, 2007 10:21 pm

Re: captcha help

Post by yanglei1979 »

Delete empty row
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: captcha help

Post 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"])
?
deeptranz03
Forum Newbie
Posts: 4
Joined: Mon Jan 14, 2008 1:41 am

Re: captcha help

Post 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"])
?
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: captcha help

Post 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...
deeptranz03
Forum Newbie
Posts: 4
Joined: Mon Jan 14, 2008 1:41 am

Re: captcha help

Post 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
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: captcha help

Post 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...
deeptranz03
Forum Newbie
Posts: 4
Joined: Mon Jan 14, 2008 1:41 am

Re: captcha help

Post 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 :)
Post Reply