Answer the question
In order to leave comments, you need to log in
How to correct captcha code?
There are 2 files
5.php
<?php header("Content-Type: text/html; charset=utf8"); ?>
<form action="" method="post">
<input type="text" name="coded"><p />
<img src="6.php" alt="" /><p />
<input type="submit" name="subu" value="отправить"><p />
</form>
<?php
$code_capcha = $_POST['coded'];
if(isset($_POST['subu'])) {
if($code_capcha == $_COOKIE['coded']) {
echo "<font color='green'> Верно </font>";
}
else {
echo "<font color='red'> Неверный код </font>";
}
}
?>
<?php
header("Content-Type: image/png;");
setcookie("coded");
$image = imagecreatetruecolor(120, 30);
$red = imagecolorallocate($image, 255, 0, 0);
$black = imagecolorallocate($image, 0, 0, 0);
$w = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $red);
$cap_array = array("a","b","c","d","e","f","g","h","j","k","l","m","n","o","p","q",
"r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
for($i = 0; $i <= 4; $i++) {
$code_capcha .= $cap_array[rand(0, count($cap_array) - 1)];
}
$_COOKIE['coded']= $code_capcha;
imagestring($image, 5, 40, 5, $code_capcha, $w);
imagepng($image);
imagedestroy($image);
?>
Answer the question
In order to leave comments, you need to log in
in 6.php remove setcookie("coded");
which is at the top.
instead of $_COOKIE['coded']= $code_capcha;
writing
And in general, this is the wrong approach. The bot can read the value of your cookie. It is better to save in session.
setcookie("coded");
Programmer: Dear php, put the captcha value in this cookie.
*Demonic laughter, hellfire reflections*
php: Muahaha, he thinks he's writing to the cookie this way, but he's really just updating the global array with past cookie data. I'll just throw out this array at the end of the script and the user will get an empty cookie that the programmer set initially! Brilliant plan!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question