doudou5023 2011-09-08 21:44
浏览 64
已采纳

使用PHP和jQuery进行ReCaptcha验证

I'm trying to make a good registration form. At first I made a PHP validation script, because I know for sure that it is secure. When I posted my form incorrectly and got an error returned, all my input values were gone. I didn't like that so I learned about validation with Javascript. I don't have much experience with Javascript, but I've managed to validate my form with both client-side and server-side validation, except for the ReCaptcha field.

The problem is that if I fill in the code correctly and press Submit, the client-side verification returns 'Succes' but the server-side validation always returns 'Wrong'.

If I only validate with the server-side script it will return 'Succes' though, so the script itself is fine but somehow they won't work after each other with the same code.

Client-side recaptcha validation script:

function validateCaptcha()
{
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
//console.log(challengeField);
//console.log(responseField);
//return false;
var html = $.ajax({
    type: "POST",
    url: "handlers/ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;

//console.log( html );
if(html == "success") {
    //Add the Action to the Form
    $("form").attr("action", "handlers/register_handler.php");
    //Indicate a Successful Captcha
    $("#captchaStatus").html("Success!");
    // Uncomment the following line in your application
    return true;
} else {
    $("#captchaStatus").html("The security code you entered did not match. Please try again.");
    Recaptcha.reload();
    return false;
}
}   

ajax.recaptcha.php

<?php
require_once('recaptchalib.php');
$privatekey = "private key";
$resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
echo "success";
} else {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
   "(reCAPTCHA said: " . $resp->error . ")");
}
?>

Server-side recaptcha validation script:

require_once('recaptchalib.php');
$privatekey = "private key";
$resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
echo "error"; }
else { echo "succes"; }

They're both allmost the same, so I really don't know what I'm doing wrong here.

Please help me out because I don't want to enter all the information again when I've only got the Captcha code wrong.

Thanks in advance

EDIT: I've discovered the fact that the same challenge field code is used for both client-side verification as the server-side verification. Maybe it won't work because the challenge field code can only be used once? If so, is there a way to make this work for me?

  • 写回答

3条回答 默认 最新

  • doufen1890 2011-09-09 02:09
    关注

    reCaptcha intentionally rejects CAPTCHAs which are submitted to their server more than once (e.g, by both the client-side and server-side validation). You will need to bite the bullet and rewrite your register_handler.php script to either redisplay form input or write it as an AJAX action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题