jQuery 验证 +安全映像问题
我在使用 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请求,ajax工作正常,但是当我发送表单时,$securimage->check($_POST['captcha'])
在sendform.php
返回 false
。然后我尝试禁用远程 capctha 验证,sendform.php
中的 viola $securimage->check($_POST['captcha'])
返回 true!
正如您所看到的,我echo
sendform.php
中的一些值,结果是:
案例 #1:AJAX 验证码验证已启用。
结果:
echo $_POST['captcha']; // User_input_value;
echo $securimage->getCode(); // nothing
$securimage->check($_POST['captcha']) // false
案例#2:AJAX 验证码验证已禁用。
结果:
echo $_POST['captcha']; // User_input_value;
echo $securimage->getCode(); // code from image
$securimage->check($_POST['captcha']) // true (if equals)
有人知道如何使其发挥作用吗?
感谢您的任何建议。
I have some strange issue using jQuery Validation plugin. Firs of all here is my code:
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
}
?>
So, the problem is when I'm checking security code
with AJAX request, ajax works fine, but when I'm send the form, $securimage->check($_POST['captcha'])
in sendform.php
returns false
. Then I tried to disable remote capctha validation and viola $securimage->check($_POST['captcha'])
in sendform.php
returned true
!
As you can see I echo
some values in sendform.php
and result is:
Case #1: AJAX captcha validation enabled.
Results:
echo $_POST['captcha']; // User_input_value;
echo $securimage->getCode(); // nothing
$securimage->check($_POST['captcha']) // false
Case #2: AJAX captcha validation disabled.
Results:
echo $_POST['captcha']; // User_input_value;
echo $securimage->getCode(); // code from image
$securimage->check($_POST['captcha']) // true (if equals)
Anyone know how to make it work?
Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了防止重置 captch,您应该验证自己,而不是在 process.php 中调用 check() 函数,如下代码所示
To prevent resetting captch, you should validate yourself without calling check() function in process.php like below code
几乎相同的 问题 不久前被问过,似乎每次检查后验证码都会重置。
我建议在会话中设置一个标志,在有效的验证码之后,在
process.php
中将其设置为TRUE
,然后检查它而不是$ secureimage->check($_POST['captcha'])
在您的sendform.php
中:并且:
现在这里有两个注意事项:
sendform.php
中进行另一次验证码检查当然有人可能会向您发送垃圾邮件,但是如果您确实需要使用 Ajax,那么您必须停止处理验证码jQuery 插件,并在提交表单时验证它,就像原始 文档< /a> 方法。
Almost same question was asked a while ago, it seems that the captcha is resetting after each check.
What I suggest is to have a flag in your session that you would set to
TRUE
in yourprocess.php
after a valid captcha and then checking it instead of$securimage->check($_POST['captcha'])
in yoursendform.php
:And:
Now here are two notes:
sendform.php
Of course someone could spam you, but then and if you really need to use Ajax, then you have to stop processing the captcha in the jQuery plugin and just validate it when you submit your form, just like the original documentation approach.