Help us understand the problem. What is going on with this article?

GoogleのreCAPTCHAをPHPで使う

More than 5 years have passed since last update.

目的

Googleが提供している新しいreCAPTCHAAPIを使ってみる。
https://www.google.com/recaptcha/intro/index.html

Site key/SecretKey発行

管理画面にアクセスしてSite key/SecretKeyを発行する
https://www.google.com/recaptcha/admin
Label:なんでも良さげ
Domains:使用するサイトのドメイン。1行1ドメイン。

クライアントサイド

javascriptを読み込み
<script src='https://www.google.com/recaptcha/api.js'></script>

HTML記述
<div class="g-recaptcha" data-sitekey="{{SITEKEY}}"></div>

サーバサイド

GoogleAPIに対してリクエストを発行します。

recaptcha.php
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => '{{SECRETKEY}}',
    'response' => $_REQUEST['g-recaptcha-response'],
    'remoteip' => $_SERVER['REMOTE_ADDR'],
);
$url .= '?' . http_build_query($data);
$header = Array(
    'Content-Type: application/x-www-form-urlencoded',
);
$options = array('http' =>
    array(
        'method' => 'GET',
        'header'  => implode("\r\n", $header),
        'ignore_errors' => true
    )
);
$apiResponse = file_get_contents($url, false, stream_context_create($options));
$jsonData = json_decode($apiResponse, TRUE);
if($jsonData['success'] !== TRUE){
    echo '失敗した';
}

まとめ

簡単にCAPTCHAが実装できます。
さらに、正規ユーザが読めない文字を解読する必要がないのでいいと思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした