Php captcha returns old captcha value
-
13-10-2022 - |
Question
I am following this captcha tutorial with a partial success, actually I managed to make it work but now I need to validate the form by Javascript, in order to to that I have the following PHP script.
<? session_start(); ?>
<html>
<head>
<title>TEST</title>
</head>
<body>
<?php
echo "\n<script type=\"text/javascript\">";
echo "\n\tfunction validateCaptcha(captcha){ ";
echo "\n\t alert(captcha); }";
echo "\n</script>";
?>
<form action=next.php method=get onsubmit="return validateCaptcha('<?php
$catp = $_SESSION['captcha'];
echo $catp;
?>')"/>
<img src="captcha.php" /><br />
<input type="text" name="answer" placeholder="Enter captcha here" />
<input type="submit" name="send" value="Send" />
</form>
</body>
</html>
The problem is that each time I press on the button Send the form is not validated because the captcha value is the old one, I've been struggling with it a couple of hours I could not figure out how to make it work. For me it is weird because the Javascript code generated has the good value.
I also tried using hidden variables such as:
<input id="checkCaptcha" name="checkCaptcha" type="hidden" value="<?php
$catp = $_SESSION['captcha']; echo $catp; ?>" />
but I did not succeed.
By the way this is the captcha.php code:
<?php
session_start();
header("Expires: Tue, 01 Jan 2013 00: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");
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 5; $i++)
{
$randomString .= $chars[rand(0, strlen($chars)-1)];
}
$_SESSION['captcha'] = strtolower( $randomString );
$im = @imagecreatefrompng("captcha_bg.png");
imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString);
header ('Content-type: image/png');
imagepng($im, NULL, 0);
imagedestroy($im);
?>
I know I could use reCaptcha but I would prefer solve it before.
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow