LoginSignup
5
5

More than 3 years have passed since last update.

google API で迷惑メール対策 recaptcha v3

Last updated at Posted at 2019-08-02

recaptcha v3 って何?

メールや会員登録のいたずらを防ぐ。
今までの recaptcha と違って 画像やチェックボックスが出ずにユーザーの行動で判断する。

登録

から以下を取得

サイトキー:hogehoge
シークレットキー:secsec

使い方

・リキャプチャが読み込まれたら recaptchaResponse をセット

index.html

//1
<script src="https://www.google.com/recaptcha/api.js?render=hogehoge"></script>
<script>
    grecaptcha.ready(function() {
        grecaptcha.execute('hogehoge').then(function(token) {
            var recaptchaResponse = document.getElementById('recaptchaResponse');
            recaptchaResponse.value = token;
        });
    });
</script>


//2
<form>
<input type="hidden" name="recaptchaResponse" id="recaptchaResponse" />

ここに送信ボタンとか。色々。。。

</form>


php側

・送信されてきた recaptchaResponse とリキャプチャデータをチェック
・OKなら送信

send.php

if (!empty($_POST['recaptchaResponse'])) {
    $secret = 'secsec';
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['recaptchaResponse']);
    $reCAPTCHA = json_decode($verifyResponse);
    if ($reCAPTCHA->success) {

        // たぶん人間なので送信
        echo $_POST['recaptchaResponse']." トークン 送信しました";
        die;

    } else {
        // ボットかも

        echo "Error: BOT送信の可能性があります。再度入力してください";
        die;
    }
}


5
5
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
5
5