0
0

More than 1 year has passed since last update.

reCAPTCHA v3 設置方法

Posted at

クライアントサイドの設定

headタグ内にAPI実行スクリプト記載
"recap"の部分はフォームのIDを設定する

<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
   function onSubmit(token) {
     document.getElementById("recap").submit(); //フォームID設定
   }
</script>

フォームタグに関連付け

<form action="index.php" method="post" class="contact_form" id="recap">

送信ボタンの設定

<button class="g-recaptcha" data-sitekey="サイトキー" data-callback="onSubmit" data-action="submit">送信</button>

サーバーサイドの設定

PHPにデータ受け取りと条件判定のプロセスを実行
$secret_key 変数にシークレットキーが入る

$recap_response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. $secret_key .'&response=' . $_POST['g-recaptcha-response']);
$recap_response = json_decode($recap_response); // JSON形式をPHPオブジェクトにデコード
         // successプロパティ(trueかfalseか)で判定
    if ($recap_response->success == true) {
            // 合格の場合の処理
        } else {
            // 不合格の場合の処理
            echo "Error: BOT送信の可能性があります。再度入力してください";
                    die;
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0