hexageek1337 / recaptcha-codeigniter4
此包已被废弃,不再维护。未建议替代包。
免费使用 Library reCAPTCHA v2 for Codeigniter 4。
1.0
2021-04-10 06:37 UTC
Requires
- codeigniter4/framework: >=4.1.1
README
免费使用 Library reCAPTCHA v2 for Codeigniter 4。
注意
此存储库已迁移至 https://github.com/PHPDevsr/reCaptcha-Codeigniter4
什么是 reCAPTCHA?
reCAPTCHA 是一个免费服务,可保护您的网站免受垃圾邮件和滥用。它使用高级风险评估引擎来区分人类和机器人。通过新的 API,大量有效的用户将无需解决 CAPTCHA 即可通过 reCAPTCHA 挑战(请参阅博客了解详细信息)。reCAPTCHA 以小部件的形式提供,您可以轻松将其添加到博客、论坛、注册表单等。
查看 详细信息。
注册 API 密钥对
要使用 reCAPTCHA,您需要为您的网站在 http://www.google.com/recaptcha/admin 上注册一个 API 密钥对。密钥对由站点密钥和密钥组成。站点密钥用于在您的网站上显示小部件。密钥授权您的应用程序后端与 reCAPTCHA 服务器之间的通信以验证用户的响应。出于安全目的,密钥需要保持安全。
安装
使用 composer 安装
$ composer require hexageek1337/recaptcha-codeigniter4
使用 git clone 手动安装
$ git clone https://github.com/hexageek1337/reCAPTCHA-Codeigniter
$ cd reCAPTCHA-Codeigniter-main
$ cp app/Config/Settings.php your_application_folder/Config/
$ cp app/Libraries/Recaptcha.php your_application_folder/Libraries/
使用
在 app/Config/Settings.php
文件中设置您的站点密钥和密钥
public $recaptcha_site_key = ''; public $recaptcha_secret_key = ''; public $recaptcha_lang = 'id';
渲染 reCAPTCHA 小部件
echo $this->recaptcha->getWidget();
输出
<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'));
通过传递数组参数更改默认类型
echo $this->recaptcha->getWidget(array('data-theme' => 'dark', 'data-type' => 'audio'));
渲染脚本标签
echo $this->recaptcha->getScriptTag();
输出
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=onload&hl=id" defer></script>
通过传递数组参数更改渲染值
echo $this->recaptcha->getScriptTag(array('render' => 'explicit'));
通过传递数组参数更改默认语言
echo $this->recaptcha->getScriptTag(array('render' => 'explicit', 'hl' => 'zh-TW'));
验证响应
调用 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!"; }
致谢
- Bo-Yi Wu @appleboy
- Denny Septian Panggabean github.com/hexageek1337