Google Recaptcha v3 example demo

后端 未结 7 924
故里飘歌
故里飘歌 2020-12-02 10:44

Until now, I was working with Google Recaptcha v2, but now I want to update my WebApp using the lastest version (v3).

Is it possible to anyone add a fully working Go

7条回答
  •  时光说笑
    2020-12-02 11:10

    Simple code to implement ReCaptcha v3

    The basic JS code

    
    
    

    The basic HTML code

    .... your fields

    The basic PHP code

    if (isset($_POST['g-recaptcha-response'])) {
        $captcha = $_POST['g-recaptcha-response'];
    } else {
        $captcha = false;
    }
    
    if (!$captcha) {
        //Do something with error
    } else {
        $secret   = 'Your secret key here';
        $response = file_get_contents(
            "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
        );
        // use json_decode to extract json response
        $response = json_decode($response);
    
        if ($response->success === false) {
            //Do something with error
        }
    }
    
    //... The Captcha is valid you can continue with the rest of your code
    //... Add code to filter access using $response . score
    if ($response->success==true && $response->score <= 0.5) {
        //Do something to denied access
    }
    

    You have to filter access using the value of $response.score. It can takes values from 0.0 to 1.0, where 1.0 means the best user interaction with your site and 0.0 the worst interaction (like a bot). You can see some examples of use in ReCaptcha documentation.

提交回复
热议问题