oppara/cakephp-simple-recaptcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oppara/cakephp-simple-recaptcha

最新稳定版本:0.6.1

Composer 安装命令:

composer require oppara/cakephp-simple-recaptcha

包简介

CakePHP plugin to handle Google Recaptcha V3

README 文档

README

Build Status

CakePHP plugin to handle Google Recaptcha

If both v2 and v3 are configured, it will act as v3 and use v2 as a fallback.
If only v3 is configured, it will act as v3.
And V2 so.

Requirements

  • PHP 8.1+
  • CakePHP 5.0+

Installation

composer require oppara/cakephp-simple-recaptcha

Load plugin

bin/cake plugin load Oppara/SimpleRecaptcha

Usage

Use V3 with V2 fallback

Click to expand

config/app.php

   'Recaptcha' => [
        'v3' => [
            'site_key' => 'your_site_key',
            'secret_key' => 'your_secret',
        ],
        'v2' => [
            'site_key' => 'your_site_key',
            'secret_key' => 'your_secret',
        ],
    ],

src/Controller/InquiryController.php

<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;
use Oppara\SimpleRecaptcha\Exception\RecaptchaV3Exception;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha', [
            'actions' => [
                'input',
            ],
        ]);
    }

    public function input()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (RecaptchaV3Exception $e) {

                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('You have been identified as a robot. Please try again.');

                return $this->redirect(['action' => 'input']);

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}

templates/layout/defalult.php


<?= $this->fetch('scriptBottom'); ?>
</body>
</html>

templates/Inquiry/input.php

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->button('submit') ?>
<?= $this->Recaptcha->hidden(); ?>
<?= $this->Recaptcha->checkbox(); ?>
<?= $this->Form->end(); ?>

Use v3

Click to expand

config/app.php

   'Recaptcha' => [
        'v3' => [
            'site_key' => 'your_site_key',
            'secret_key' => 'your_secret',
        ],
    ],

src/Controller/InquiryController.php

<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha');
    }

    public function index()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}

templates/layout/defalult.php


<?= $this->fetch('scriptBottom'); ?>
</body>
</html>

templates/Inquiry/input.php

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->button('submit') ?>
<?= $this->Recaptcha->hidden(); ?>
<?= $this->Form->end(); ?>

Use v2

Click to expand

config/app.php

   'Recaptcha' => [
        'v2' => [
            'site_key' => 'your_site_key',
            'secret_key' => 'your_secret',
        ],
    ],

src/Controller/InquiryController.php

<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;
use Oppara\SimpleRecaptcha\Exception\RecaptchaV3Exception;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha', [
            'actions' => [
                'input',
            ],
        ]);
    }

    public function input()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}

templates/layout/defalult.php


<?= $this->fetch('scriptBottom'); ?>
</body>
</html>

templates/Inquiry/input.php

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->button('submit') ?>
<?= $this->Recaptcha->checkbox('data-callback="verifyCallback" data-expired-callback="expiredCallback"'); ?>
<?= $this->Form->end(); ?>

License

Licensed under the MIT License.

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-14