2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google reCAPTCHAを設置する

Last updated at Posted at 2020-03-27

個人的な備忘録として

#APIの取得

ここからいろいろ設定
Google reCAPTCHA

#HTML

recaptcha.html
<html>
<head>
<title></title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<form action="" method="post">
    <div class="g-recaptcha" data-sitekey="サイトキーを入力"></div>
</form>
</html>

#php

Ninshou.php
    public function login() {
        if ($this->request->is('post')) {
            //RECAPTCHAからのResponseデータを受ける
            $recaptchaResponse = $_POST['g-recaptcha-response'];
            $secretKey = "セキュリティーキーを入力";

            //reCAPTCHA
            $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$recaptchaResponse}");

            $responseKeys = json_decode($response,true);
            if(intval($responseKeys["success"]) !== 1) {
                $this->Session->setFlash('「私はロボットではありません」にチェックを入れてください。');
            } else {
                if ($this->Auth->login()) {
                    $this->Session->setFlash('ログインに成功しました。');
                } else {
                    $this->Session->setFlash('ユーザー名かパスワードを確認してください。');
                }
            }

        }
    }
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?