PHP - 关于将 reCAPTCHA 与 jQuery 结合使用的问题
这是一个教程,说明如何将 jQuery 表单验证与 reCAPTCHA 结合起来。 http://snipplr.com/view/15563/jquery-validating -recaptcha-with-ajax/
根据我的理解,上面的教程实际上是通过与服务器 reCAPTCHA 脚本通信的 aJax 进行客户端验证。
验证成功后,我使用从注释中借用的以下代码
$('#formID').validate({
submitHandler: function(form) {
if(validateCaptcha()){ // Submit form
offerForm.ajaxSubmit(); } } });
来提交表单,请参阅原始代码的第21行:
$("form").attr("action", "http://action/to/the/form_handler.php");
我的问题是我是否必须在 form_handler.php 中调用 recaptcha_check_answer 并传入参数
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
如果不是,那么人们可以通过更改验证程序轻松避免 reCAPTCHA。 看来我们总是要同时进行客户端+服务器验证,这是同样的想法。
如果我的理解有误,请纠正我的想法。
// 提供我遇到的问题的详细信息 ///
<code>
<form id="regFormBody" method="post" action="verify.php">
...
</code>
$("#regFormBody").validate({
debug: true,
errorPlacement: function (error, element) {
error.insertAfter(element.parents('div.collection:first'));
},
rules: {
loginemail: { required: true, email: true, rangelength: [4, 32] },
password: { required: true, rangelength: [8, 30], passwordPattern: true },
confirmpassword: { required: true, rangelength: [8, 30], equalTo: "#password" }
}
}
});
这是我遇到的问题: 如果表单通过了客户端验证,那么它根本不会触发 verify.php 并在验证后停止。 谢谢
Here is a tutorial that indicates how to combine jQuery Form Validation with reCAPTCHA.
http://snipplr.com/view/15563/jquery-validating-recaptcha-with-ajax/
Based on my understanding, the above tutorial in fact does a client side validation through aJax that communicates with the server reCAPTCHA script.
After the validation is successful, I use the following code borrowed from the comments:
$('#formID').validate({
submitHandler: function(form) {
if(validateCaptcha()){ // Submit form
offerForm.ajaxSubmit(); } } });
to submit the form and please see line 21 of the original code:
$("form").attr("action", "http://action/to/the/form_handler.php");
My question is whether or not I MUST call recaptcha_check_answer inside form_handler.php with passed in parameters
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
If not, then a person can easily avoid the reCAPTCHA by changing the validation procedure.
It seems that the same idea that we always have to both client+server validation.
Please correct my idea if I misunderstand.
// Give detail information for the issue I have ///
<code>
<form id="regFormBody" method="post" action="verify.php">
...
</code>
$("#regFormBody").validate({
debug: true,
errorPlacement: function (error, element) {
error.insertAfter(element.parents('div.collection:first'));
},
rules: {
loginemail: { required: true, email: true, rangelength: [4, 32] },
password: { required: true, rangelength: [8, 30], passwordPattern: true },
confirmpassword: { required: true, rangelength: [8, 30], equalTo: "#password" }
}
}
});
Here is the problem I have:
If the form passes the client side validation, then it doesn't NOT trigger the verify.php at all and stops after the validation.
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这对我来说听起来是正确的。是的,您肯定需要在服务器上验证验证码。我根本不喜欢验证验证码客户端的想法,并且我认为您也不希望在用户可以获取的脚本中发布您的 reCaptchi API 密钥。另外,我预计相同验证码值的第二次验证(客户端检查后的服务器端检查)无论如何都会被 recaptcha 的服务器拒绝 (从原始博客的评论中确认了这一点) 。
因此,我认为您需要将验证码发布到 AJAX 操作处理程序,它应该执行验证以及您的操作。您可以在提交之前验证用户是否已输入验证码,但在我看来,您根本不应该尝试在客户端验证它。
Yes, that sounds correct to me. Yes, you definitely need to validate the captcha on the server. I don't like the idea of validating the captcha client-side at all and I don't think you want to be posting your reCaptchi API keys in a script that user can get hold of either. Also I'd expect the second validation of the same captcha values (your server-side check after the client-side checke) would get rejected by recaptcha's servers anyway (confirmation of this from a comment on the original blog).
So I think you need to post the captcha to your AJAX action handler and it should do the validation as well as your action. You could validate the user has entered something for the captcha before you submit it but IMO you shouldn't try and validate it client side at all.