| この記事の要点 |
|---|
|
本稿はreCAPTCHA v3 使い方について説明します。
前提
公式ドキュメントは以下のURLを参照。
https://developers.google.com/recaptcha/intro
reCAPTCHA v3を使用するにはキー登録が必要となる。
以下のURLからキー登録ができる。
headタグ
headタグ内に以下の記述をする。
|
<script src='https://www.google.com/recaptcha/api.js?render=Site Key'></script> |
Site keyは自身のものを入力する。
formの作成
|
<!-- 送信ボタン --> </form> <!-- reCAPCTHA --> <script> function commentSubmitFunc() { grecaptcha.ready(function() { grecaptcha.execute('Site Key', {action: 'Action Name'}) .then(function(token) { $('#test_form').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">'); $('#test_form').submit(); }); }); }</script> |
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 keyとtokenを渡すことでレスポンスを取得できる。
以下、レスポンスの内容。(公式サイトより抜粋)
|
{ "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 } |
ポイントはsuccessとscoreの値。
scoreの幅は1.0~0.0で低いほどbotである可能性が高くなる。
この値を見てbotかどうかを判断する必要がある。
※しきい値について、Google の公式ドキュメントは「既定では 0.5 を使える」としたうえで、管理コンソールで実際のスコア分布を見て調整することを勧めている。
子ページはありません
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスのソフト一覧|Blender・Metasequoiaと用途別の選び方
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Visual Studio「デザイナーを表示できません」エラーの原因と対処 2026-10-28 12:06:00
- SSH 認証に失敗しました。再試行してください の対処 2026-10-28 12:06:00
- SSH2 秘密鍵の読み込みに失敗しました の原因と対処 2026-10-28 12:06:00
- TeraTerm のよくあるエラーと対処法完全ガイド — Connection refused / 2026-10-28 12:06:00
- Visual Studio エラー一覧|コンパイルエラー・警告の確認方法と対処 2026-10-28 11:39:57
- Whitelabel Error Page の原因と消し方|Spring Boot のエラーページ 2026-10-28 11:39:57
- Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法 2026-10-28 11:39:57
- SQL WHERE 句完全ガイド 2026-10-28 11:39:57
- SQL JOIN 完全ガイド (INNER/LEFT/RIGHT/FULL OUTER) 2026-10-28 11:39:57
- tomcat の起動 / 停止ログと catalina.log・catalina.out の違い 2026-10-28 10:24:23
- Tomcat の起動確認方法 5 つ|localhost:8080・ポート・プロセス・ログ 2026-10-28 10:24:23
- Oracle 19c COMMITの使い方: 暗黙コミット・自動確定・応答不明時の確認 2026-10-28 09:59:09
- Oracle 19c DELETE文|削除対象の確認・COMMIT前の判断・ROLLBACKの条件 2026-10-28 09:59:09
- Oracle 19c INSERT文|INSERT ALL・SELECT・RETURNINGとMERGEの使い分け 2026-10-28 09:59:09
- Oracle 19cのトランザクション制御|COMMIT・ROLLBACK・分離レベルと接続設定 2026-10-28 09:59:09