ReCaptcha 2.0 With AJAX

后端 未结 1 853
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 18:31

I have managed to get ReCaptcha 2.0 working in my website. However, it\'s only working when I don\'t use AJAX and I let the form submit \"naturally\".

I want to su

相关标签:
1条回答
  • 2020-12-07 19:27

    Ok, this was pretty silly.

    I have done a couple of things wrong:

    • In the PHP file, all the strings had single quotes on them, and that caused problems.
    • Throughout the testing, I added multiple printings of things in the PHP file, thus the if (status == "ok") was never working. I did get the emails but did not get any conformation that I did and now I see why.
    • When I wanted to check what the PHP file was omitting, I simply went to it's address in the URL and always got an error. Even when the mails were sent. Now I understand that that is not the correct way of checking the logs.

    Thanks to @Samurai who helped my figure out things.


    Final PHP code:

    <?php
        // assemble the message from the POST fields
    
        // getting the captcha
        $captcha = "";
        if (isset($_POST["g-recaptcha-response"]))
            $captcha = $_POST["g-recaptcha-response"];
    
        if (!$captcha)
            echo "not ok";
        // handling the captcha and checking if it's ok
        $secret = "MY_SECRET";
        $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), true);
    
        // if the captcha is cleared with google, send the mail and echo ok.
        if ($response["success"] != false) {
            // send the actual mail
            @mail($email_to, $subject, $finalMsg);
    
            // the echo goes back to the ajax, so the user can know if everything is ok
            echo "ok";
        } else {
            echo "not ok";
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题