Reputation: 9
I just want to know what should i put more in order to guess if captcha is wrong or not in my $_POST (the captcha image works fine in my form) so my question is how to put the errors if the user input a wrong captcha in my captcha image?
if ($_POST['captcha'] !== $_POST['real']) {
$errors[] = 'You're wrong';
}
Here is my captcha code
session_start();
function random($length) {
$chars = "abcdefghijklmnopqrstuvwxyz23456789";
$str = "";
$size = strlen($chars);
for ($i=0;$i<$length;$i++) {
$str .=$chars [rand (0, $size-1)];
}
return $str;
}
$cap = random(7);
$_SESSION['real'] = $cap;
$image = imagecreate(100, 20);
$background = imagecolorallocate ($image, 0,0,0);
$foreground = imagecolorallocate ($image, 255,255,255);
imagestring($image, 5,5,1,$cap,$foreground);
header("Content-type: image/jpeg") ;
imagejpeg ($image);
Upvotes: 0
Views: 316
Reputation: 3005
Your image is very clear. Easy to read for humans. Also easy to read for bots. I tested your code and put the image through a simple online OCR (http://www.i2ocr.com/ ) and it came back with the test perfectly.
Try it yourself.
I would consider using an existing captcha library. Such as the Google Captcha options. I like the no captcha recaptcha ( http://googleonlinesecurity.blogspot.com/2014/12/are-you-robot-introducing-no-captcha.html )
--- Edit to answer this case.
In your captcha processing file
session_start();
$errors[]=array();
if ($_POST['captcha'] != $_SESSION['real']) {
$errors[] = 'Sorry. Incorrect Response. Please Try Again';
}
if(count($errors)>0){
//show user the errors
print_r($errors); //You should make this pretty
//probably go back to the login form
echo'<a href="loginform.php">Go Back and Try Again</a>';
//stop processing
exit();
)
//if we got here then the posted captcha matche the 'real' session variable.
echo'Yay! You got it right!';
Upvotes: 1