jquery验证和php验证码问题

时间:2010-12-26 11:36:26

标签: php jquery

我的验证码字段

<p style="height:30px;"><img id="captcha" src="includes/secureimage/securimage_show.php?sid=<?php echo md5(time()); ?>" alt="CAPTCHA Image" />
<a tabindex="-1" style="border-style: none; margin-left:5px;" href="#" title="Refresh Image" onclick="document.getElementById('captcha').src = 'includes/secureimage/securimage_show.php?sid=' + Math.random(); return false"><img src="images/refresh.png" alt="Reload Image" onclick="this.blur()" style="vertical-align: top; border:0;" /></a></p></div>
<p><label for="seccode">Verification Code:<span class="important">*</span></label><input id="seccode" type="text" name="seccode" size="10" class="required" /></p>

我的jquery验证验证码的插件代码

seccode: {
        required: true,
        remote: {
        url: "checkuser.php",
        type: "post",
        data: {
        seccode: function() {
            //alert($("#seccode").val());
            return $("#seccode").val();
        }
    }
}
}

我的php

if($_POST['seccode'])
    {
        $securimage = new Securimage();
        return $securimage->check($_POST['seccode']);
    }

我正在使用securimage进行验证码。验证码的必要条件的消息正在运行。但是远程条件不起作用。当然我已经在php中包含了必需的类文件,并且还在php文件的开头启动了session_start()。

[注] 错误现在消失了。现在有了新的东西。第一次验证码在我关注时工作,但之后它会继续回调每次更改的验证方法..请参阅下面的firebug输出...

GET http://localhost:8080/property/html/captcha.php?seccode=pzubwg
GET http://localhost:8080/property/html/captcha.php?seccode=pzubwg

200 OK
        36ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=l
GET http://localhost:8080/property/html/captcha.php?seccode=l

200 OK
        39ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=ln
GET http://localhost:8080/property/html/captcha.php?seccode=ln

200 OK
        41ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnr
GET http://localhost:8080/property/html/captcha.php?seccode=lnr

200 OK
        36ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrg
GET http://localhost:8080/property/html/captcha.php?seccode=lnrg

200 OK
        38ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgu
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgu

200 OK
        32ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgue
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgue

200 OK
        25ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgu
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgu

200 OK
        34ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrg
GET http://localhost:8080/property/html/captcha.php?seccode=lnrg

200 OK
        40ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgv
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgv

200 OK
        42ms    
jquery.min.js (line 140)
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgve
GET http://localhost:8080/property/html/captcha.php?seccode=lnrgve

200 OK
        37ms

2 个答案:

答案 0 :(得分:3)

基本上,当您的jquery验证验证码时,验证码代码会在验证发生的同一个函数中被销毁。这就是我遇到问题的原因。一旦再次刷新验证码,代码就可以正常工作。

答案 1 :(得分:1)

如果远程验证成功,验证要求您返回“true”,我也更喜欢使用不同的服务器端检查进行远程验证,就像这个demo一样。

因此验证码验证将是post_captcha.php

<?php
session_start();
include_once 'securimage/securimage.php';

$securimage = new Securimage();
if ($securimage->check($_POST['seccode']) == false) {
    // the code was incorrect
    // handle the error accordingly with your other error checking
    // or you can do something really basic like this
    //die('The code you entered was incorrect.  Go back and try again.');
    echo "false";
} else {
    echo "true";
}
?>

打开演示链接并启用firebug并仅使用任何用户名提交用户名字段,然后使用其中一个用户名进行检查(例如“ asdf ”),您会注意到响应就像我的post_captcha.php回复一样!

易卜拉欣