google reCAPTCHA v2 機器人驗證

Step 1.
註冊 API key
https://www.google.com/recaptcha

google reCAPTCHA key

Step 2.
html

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="your_api_public_key"></div>

Step 3.
php

$recaptcha_secret = 'your_api_private_key';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $_POST['g-recaptcha-response']);
$response = json_decode($response, true);

if ($response["success"] !== true) {
   echo '<script type="text/javascript">alert("google驗證有誤,請重新操作!");</script>';
   exit();
}

參考資料:
https://medium.com/@d101201007/google-recaptcha%E6%A9%9F%E5%99%A8%E4%BA%BA%E9%A9%97%E8%AD%89-%E6%95%99%E5%AD%B8-php%E7%AF%84%E4%BE%8B-3685a394dce2

在〈google reCAPTCHA v2 機器人驗證〉中有 1 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *