| この記事の要点 |
|---|
|
本稿は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 / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- djangoのテンプレートの作成とヘッダー・フッターの共通化 2026-10-03 21:41:49
- テンプレートフラグメント(ヘッダー等の共有化) 2026-10-03 21:41:49
- reCAPTCHA v3 使い方(サンプル付き) 2026-10-03 21:37:05
- Content-Type一覧|MIMEタイプとはとHTTPでの主な使用場面 2026-10-03 21:37:05
- SpringにおけるAOPの使い方 2026-10-03 21:37:05
- X (Twitter) API でツイートできないがエラーが出ない問題の原因と対処 2026-10-03 11:33:01
- X (Twitter) API アプリケーション登録完全ガイド|v2・Bearer Token・OAuth 2.0 PKCE 2026-10-03 11:33:01
- X (Twitter) API 完全ガイド|従量課金の料金・単価(2026年10月)と v2 移行 2026-10-03 11:32:40
- Google DeepMind とは?Gemini・AlphaFold の開発元 2026-10-03 11:26:56
- Cursor とは?AI 統合型コードエディタの使い方・料金 2026-10-03 11:26:56
- Claude (Anthropic) とは?AIチャットの使い方・モデルファミリ・API 2026-10-03 11:26:56
- AIベンダー一覧:OpenAI・Anthropic・Google DeepMind・Microsoft・Meta 2026-10-03 11:26:56
- クラウド・インフラ完全ガイド — AWS/Azure/GCP/Kubernetes 2026-10-03 11:26:56
- テキストエディタ完全比較 (VS Code / Cursor / Vim / Emacs / IDE) 2026-10-03 11:26:56
- プログラミング学習プラットフォーム|Scratch・micro:bit ほか 2026-10-03 11:26:56