Submit Button Sends Email even If reCaptcha has been done

前端 未结 1 736
感动是毒
感动是毒 2020-12-21 17:26

Im in the process of adding the reCaptcha from google to my form. The problem is that even though I have followed the instructions from google. I can still press the Submit

1条回答
  •  礼貌的吻别
    2020-12-21 18:24

    So we set up the form and make sure your library is included, I prevent the submit button from being clicked while the recaptcha has not been completed and show a tooltip to notify the user it is needed to continue. Then enable it when it has been complete using the callback methods.

    login.php

    Login

    Now we post to loginHandler.php, or wherever your form submits too and then there we will assign your secret key and then verify the request with google.

    loginHandler.php

    $secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    if (isset($_POST["g-recaptcha-response"])) {
    
        $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secret) .
                '&response=' . urlencode($_POST['g-recaptcha-response']) . '&remoteip=' . urlencode($_SERVER['REMOTE_ADDR']);
        //ip address is optional
        $result = json_decode(file_get_contents($url), true);
    
        if ($result != null && $result['success'] === true) {
    
            //success, handle login/submit data or whatever
    
        } else {
            //response is bad, handle the error
            header('Location: login.php?error=4');
        }
    } else {
        //captcha response is not set, handle error
        header('Location: login.php?error=5');
    }
    

    0 讨论(0)
提交回复
热议问题