我使用jQuery Validation插件时有一些奇怪的问题。这里的所有内容都是我的代码:
formvalid.js
var v = jQuery("#sendform").validate({
rules: {
/* some other rules */
captcha: {
required: true,
remote: "securimage/process.php"
}
},
messages: {
/* some other messages */
captcha: {
required:"Security code is required",
remote:"Security code is incorrect!"
}
}
});
process.php
<?php
/* I tried with and without session_start().
Both ways works with same probelm (read next)*/
//session_start();
include_once '../securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_GET['captcha']) == false)
echo "false";
else
echo "true";
?>
sendform.php
<?php
include_once 'securimage/securimage.php';
//echo $_POST['captcha'];
$securimage = new Securimage();
//echo "<br/>".$securimage->getCode();
if($_POST['captcha'] && $securimage->check($_POST['captcha']))
{
//do sending
}
?>
所以,问题是当我用AJAX请求检查security code
时,ajax工作正常,但是当我发送表单时,$securimage->check($_POST['captcha'])
中sendform.php
会返回{{1} }}。然后我尝试禁用远程capctha验证,false
中的$securimage->check($_POST['captcha'])
中提取sendform.php
!
正如您可以看到true
echo
中的一些值,结果是:
结果:
sendform.php
结果:
echo $_POST['captcha']; // User_input_value;
echo $securimage->getCode(); // nothing
$securimage->check($_POST['captcha']) // false
任何人都知道如何让它发挥作用?
感谢您的任何建议。
答案 0 :(得分:2)
为防止重置captch,你应该在不调用process.php中的check()函数的情况下验证自己,如下面的代码
<?php
include_once 'securimage.php';
if(!isset($_GET['txtcaptcha']))
return 'false';
$securimage = new Securimage();
$securecode = $securimage->getCode();
if (strtolower($securecode) != strtolower($_GET['txtcaptcha']))
echo "false";
else
echo "true";
?>
答案 1 :(得分:1)
刚才问过几乎相同的question,似乎验证码在每次检查后都会重置。
我建议您在会话中设置一个标记,在有效的验证码后设置为TRUE
process.php
,然后在$securimage->check($_POST['captcha'])
中检查sendform.php
1}}:
if ($securimage->check($_GET['captcha']) == false) {
$_SESSION['valid'] = FALSE;
echo "false";
} else {
$_SESSION['valid'] = TRUE;
echo "true";
}
并且:
if($_POST['captcha'] && isset($_SESSION['valid']) && $_SESSION['valid']) // set it back to false inside this!
现在有两个注释:
sendform.php
当然有人可能会给你发垃圾邮件,但是如果你真的需要使用Ajax,那么你必须停止处理jQuery插件中的验证码,并在提交表单时进行验证,就像原始{{3方法。