phpdevsr/recaptcha-codeigniter4

免费使用的 reCAPTCHA v2 库,适用于 Codeigniter 4

2.1.1 2024-02-14 08:54 UTC

README

PHPUnit PHPStan Coverage Status Downloads GitHub release (latest by date) GitHub stars GitHub license

reCAPTCHA Codeigniter 4

免费使用的 reCAPTCHA v2 库,适用于 Codeigniter 4.

什么是 reCAPTCHA?

reCAPTCHA 是一项免费服务,可以保护您的网站免受垃圾邮件和滥用。它使用先进的风险评估引擎来区分人类和机器人。使用新的 API,您的大量有效人类用户将无需解决 CAPTCHA 就能通过 reCAPTCHA 挑战(请参阅博客获取更多详细信息)。reCAPTCHA 以小部件的形式提供,您可以轻松将其添加到您的博客、论坛、注册表单等。

查看详细信息

注册 API 密钥对

要使用 reCAPTCHA,您需要为您的网站 注册 API 密钥对。密钥对由站点密钥和密钥组成。站点密钥用于在您的网站上显示小部件。密钥授权您的应用程序后端与 reCAPTCHA 服务器之间的通信以验证用户的响应。出于安全目的,密钥需要保密。

安装

使用 composer 安装

$ composer require phpdevsr/recaptcha-codeigniter4
$ php spark config:publish

使用方法

v2.1.0 版本开始,可以使用 .env 文件进行多配置的环境配置

recaptcha.recaptchaSiteKey
recaptcha.recaptchaSecretKey
recaptcha.recaptchaLang

初始化

<?php

require 'vendor/autoload.php';

use Config\Recaptcha as RecaptchaConfig;
use PHPDevsr\Recaptcha\Recaptcha;

class Example
{
    /**
     * Config Recaptcha
     * 
     * @var RecaptchaConfig $config
     */
    protected RecaptchaConfig $config;

    /**
     * Recaptcha
     * 
     * @var Recaptcha $recaptcha
     */
    protected Recaptcha $recaptcha;

    public function __construct()
    {
        // Set Config
        $this->config = new RecaptchaConfig();

        $this->recaptcha = new Recaptcha($this->config);
    }
}

v2.0.0 版本开始,您可以使用助手

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        helper('recaptcha');

        $data = [
            'scriptTag' => getScriptTag(),
            'widgetTag' => getWidget(),
        ];

        $captcha = $this->request->getPost('g-recaptcha-response');
        $response = verifyResponse($captcha);

        if (isset($response['success']) and $response['success'] === true) {
            echo "You got it!";
        }

        return view('welcome_message', $data);
    }
}

或直接使用 service

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        $recaptcha = service('recaptcha');

        $data = [
            'scriptTag' => $recaptcha->getScriptTag(),
            'widgetTag' => $recaptcha->getWidget(),
        ];

        $captcha = $this->request->getPost('g-recaptcha-response');
        $response = $recaptcha->verifyResponse($captcha);

        if (isset($response['success']) and $response['success'] === true) {
            echo "You got it!";
        }

        return view('welcome_message', $data);
    }
}

渲染 reCAPTCHA 小部件

  • 默认
echo $this->recaptcha->getWidget();

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="light" data-type="image" data-size="normal" loading="lazy"></div>
  • 主题
echo $this->recaptcha->getWidget(array('data-theme' => 'dark'));

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="dark" data-type="image" data-size="normal" loading="lazy"></div>
  • 类型
echo $this->recaptcha->getWidget(array('data-theme' => 'dark', 'data-type' => 'audio'));

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="dark" data-type="audio" data-size="normal" loading="lazy"></div>

渲染脚本标签

  • 默认
echo $this->recaptcha->getScriptTag();

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=onload&hl=en" defer></script>
  • 渲染
echo $this->recaptcha->getScriptTag(array('render' => 'explicit'));

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=explicit&hl=en" defer></script>
  • 语言
echo $this->recaptcha->getScriptTag(array('hl' => 'id'));

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=onload
&hl=id" defer></script>

验证响应

调用 reCAPTCHA siteverify API 以验证用户是否通过了 g-recaptcha-response POST 参数。

$captcha = $this->request->getPost('g-recaptcha-response');
$response = $this->recaptcha->verifyResponse($captcha);

if (isset($response['success']) and $response['success'] === true) {
    echo "You got it!";
}

许可

本项目采用 MIT 许可协议 - 有关详细信息,请参阅 LICENSE 文件。

贡献

我们接受并鼓励社区以任何形式进行贡献。无论您是否能编码、编写文档或帮助查找错误,所有贡献都受欢迎。

使用 contrib.rocks 制作。