reCAPTCHA: why can't I check the same result twice?

北慕城南 提交于 2019-12-05 20:42:07

问题


I mean if I check first clients input and it is OK the second check of the same input is always false... Why is that?

I really need ability to check it twice (one for client side validation and second for server side validation)

Thanks in advance!!!

EDIT

Clarifying:

If user's input is ok and recaptcha returns true (I do it through ajax to my server which sends request to recaptcha's server) the form is submitting and sends via POST also 2 variables: recaptcha_challenge_field value and recaptcha_response_field value (which was already checked) and than my server asks recaptcha's server to check again this two values to do server side validation.

Jquery code:

$("#form_id").find("button").click(function(){
    var c = $("#recaptcha_challenge_field").val(),
        r = $("#recaptcha_response_field").val();

    $.ajax({
        url: "/ajax/captcha?challenge=" + c + "&response=" + r,
        dataType: "json",
        success: function(data){
            if(data['is_valid']){
                $.ajax({
                    url: "/ajax/captcha?challenge=" + c + "&response=" + r,
                    dataType: "json",
                    success: function(data){
                        if(data['is_valid']){
                            alert('OK');
                        }else{
                            alert('FAILED');
                        }
                    }
                });
            }else{
                Recaptcha.reload();
            }
        }
    });
    return false;
});

So, as you can see there are two absolutely identical operations with different result (it alerts only FAILED).


回答1:


Because it is stored in a session that is cleared when the result is submitted. On page load, a new session variable for that CAPTCHA value is generated.




回答2:


For validating Captcha twice via AJAX/Jquery and on a server page, this is my technique with PHP (the basic idea is just to restore Captcha's Session Variables cleared by the Captcha Check method if correct captcha entered on client side validation, so they will be there again for server side validation):

  1. The following may look confusing as it is one of my lazy way of reusing the client side validation on both the HTML form and the HTML form's action page.

  2. On the HTML form, add an additional POST variable (e.g. $_POST["task"] = "validate" which is not an input element in the form which will be passed on form submission to the client side validation PHP page

    $.ajax({
        type: "POST",
        url: "registration_validate.php", 
        data: ({
            task:"validate" << and other data >>
    
  3. On the client side validation PHP page

    $securimage_session_cv = $_SESSION["securimage_code_value"]["default"];
    $securimage_session_ct = $_SESSION["securimage_code_ctime"]["default"];
    if ($securimage->check($_POST['captcha_code']) == false) {
        // return false or print something
    } else {
        // if this script is called via ajax for form validation
        if(isset($_POST['task']) && trim($_POST['task']) == "validate"){
            // captcha will be cleared if valid/correct, restore them as it will be validated again on server side script
            $_SESSION["securimage_code_value"]["default"] = $securimage_session_cv;
            $_SESSION["securimage_code_ctime"]["default"] = $securimage_session_ct;
        }
    }
    
  4. On the server side validation page or form action url, you will have the Captcha Session intact for validation.



来源:https://stackoverflow.com/questions/9227512/recaptcha-why-cant-i-check-the-same-result-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!