1.

reCAPTCHA v3 使い方(サンプル付き)

編集
この記事の要点
  • reCAPTCHA v3 の使い方(v2 と異なりチェックボックス UI が出ない
  • 前提: キー登録https://g.co/recaptcha/v3)が必要
  • クライアント:

Site keyは自身のものを入力する。

 

formの作成




   
   

   
    commentSubmitFunc()
"/>

formの送信ボタンを押したらgrecaptcha.executeを呼び出すようにする。

その際にtokenをサーバーサイドに渡してやる必要がある。

Action Nameには以下のものが指定できる。(公式サイトより抜粋)

homepage See a cohesive view of your traffic on the admin console while filtering scrapers.
login With low scores, require 2-factor-authentication or email verification to prevent credential stuffing attacks.
social Limit unanswered friend requests from abusive users and send risky comments to moderation.
e-commerce Put your real sales ahead of bots and identify risky transactions.

※どなたか適切な日本語訳をお願いします。

本稿のサンプルではhomepageを指定する

 

サーバサイドの実装

$gRecaptchaResponse = $_POST["g-recaptcha-response"];

$reCaptchaUrl = "https://www.google.com/recaptcha/api/siteverify?secret=Secret key&response=" . $gRecaptchaResponse;

$response = @file_get_contents($reCaptchaUrl);

$response = json_decode($response,true);

if ($response['success'] != true or $response['score'] < 0.5 ){

    //エラー処理

}

Secret keyは自身のものを入力する。

APIにSecret keytokenを渡すことでレスポンスを取得できる。

以下、レスポンスの内容。(公式サイトより抜粋)

{

  "success": true|false,      // whether this request was a valid reCAPTCHA token for your site

  "score": number             // the score for this request (0.0 - 1.0)

  "action": string            // the action name for this request (important to verify)

  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)

  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved

  "error-codes": [...]        // optional

}

ポイントはsuccessscoreの値。

scoreの幅は1.0~0.0で低いほどbotである可能性が高くなる。

この値を見てbotかどうかを判断する必要がある。

※どの程度の値を設定することがベストなのか情報求む。人が操作するとだいたい0.9になることが多いらしい。

 

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. v3の使い方
  2. v2の使い方
  3. エラー一覧