David D.
David D.

Reputation: 367

Captcha Refresh (Future Ajax)

I've followed few tuts here and there and made my own captcha with session that I want to implement into my bigger project. The captcha should be used like everywhere else, in the contact / send email section to prevent spam repetition.

Problem - if you miss the captcha and hit back button, the captcha doesn't refresh. This is as good as its bad, and with ajax it means the captcha won't refresh the page and the captcha won't refresh on its own. Since the captcha is made in a different file, I guess calling the same file again should fix the problem.

Solution - I would want a 3 try system, if the captcha isn't guessed 3 times then the page is refreshed.

PHP CAPTCHA code:

<?php
session_start();
$text = rand(10000, 99999);
$_SESSION['captcha'] = $text;
// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 25, 25, 25);
$black = imagecolorallocate($im, 0, 0, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $white);


$font = 'Candara.ttf';

imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

imagepng($im);
imagedestroy($im);
?>

HTML TEST CODE:

<form action="submit.php" method="post">
Message: <textarea name="comment"></textarea><br>
Captcha: <input type="text" name="val_code"/>&nbsp;&nbsp;<img src="generate captcha.php"><br>
<input type="submit" value="Submit" name="Submit"/>
</form>

SUBMIT PHP CODE:

<?php
session_start();
if($_POST["val_code"] != $_SESSION['captcha'] OR $_SESSION['captcha'] == '') {
echo "<b><font style='color: purple'>Wrong captcha value. Please go back and resend the message</font></b>";
}
else
{
echo "<b><font style='color: green'>Thank you for your message!</font></b>";
}
?>

Upvotes: 1

Views: 1823

Answers (1)

rationalboss
rationalboss

Reputation: 5389

You need to use a counter that will keep track of the number of tries. Also, make sure to unset the captcha so that it can't be reused. See the codes below:

PHP CAPTCHA code:

<?php
session_start();
if ($_SESSION['tries'] < 2 && $_SESSION['captcha']) {
    $text = $_SESSION['captcha'];
} else {
    $_SESSION['tries'] = 0;
    $text = rand(10000, 99999);
    $_SESSION['captcha'] = $text;
}
// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 25, 25, 25);
$black = imagecolorallocate($im, 0, 0, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $white);


$font = 'Candara.ttf';

imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

imagepng($im);
imagedestroy($im);
?>

PHP Code:

<?php
session_start();
if ($_SESSION['tries']>=2) {
   echo "<b><font style='color: purple'>Captcha has expired! You need to go back and reload the page</font></b>";
} else if($_POST["val_code"] != $_SESSION['captcha'] OR $_SESSION['captcha'] == '') {
echo "<b><font style='color: purple'>Wrong captcha value. Please go back and resend the message</font></b>";
$_SESSION['tries']++;
}
else
{
unset($_SESSION['captcha']);
unset($_SESSION['tries']);
echo "<b><font style='color: green'>Thank you for your message!</font></b>";
}
?>

html test code:

<form action="submit.php" method="post">
Message: <textarea name="comment"></textarea><br>
Captcha: <input type="text" name="val_code"/>&nbsp;&nbsp;<img src="generate captcha.php" /><br>
<input type="submit" value="Submit" name="Submit" />
</form>

Upvotes: 1

Related Questions