简体   繁体   中英

captcha validating post in form

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);

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!';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM