【问题标题】:captcha validating post in form表单中的验证码验证帖子
【发布时间】:2015-03-13 01:50:09
【问题描述】:

我只是想知道我应该在我的 $_POST 中添加更多内容以猜测验证码是否错误(验证码图像在我的表单中可以正常工作)所以我的问题是如果用户输入如何放置错误我的验证码图片中有错误的验证码?

if ($_POST['captcha'] !== $_POST['real']) {
    $errors[] = 'You're wrong';
}

这是我的验证码

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

【问题讨论】:

    标签: php captcha


    【解决方案1】:

    您的图像非常清晰。易于人类阅读。机器人也易于阅读。我测试了您的代码并将图像通过简单的在线 OCR (http://www.i2ocr.com/) 并完美地通过测试返回。

    自己试试吧。

    我会考虑使用现有的验证码库。比如谷歌验证码选项。我喜欢没有验证码 recaptcha (http://googleonlinesecurity.blogspot.com/2014/12/are-you-robot-introducing-no-captcha.html)

    --- 编辑回答这个案例。

    在您的验证码处理文件中

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

    【讨论】:

    • 我只想为我制作一个而不使用其他互联网验证码,我只想验证验证码:(
    • @RoyLesterSantos 添加了一个编辑,可以帮助您解决发布到会话变量“真实”的验证码的匹配问题
    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 2014-01-21
    • 2013-04-29
    • 2022-01-26
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多