davodm/codeigniter4-turnstile
Composer 安装命令:
composer require davodm/codeigniter4-turnstile
包简介
CodeIgniter 4 package for integrating Cloudflare Turnstile Captcha
README 文档
README
CI4CFTurnstile is a CodeIgniter 4 library that integrates Cloudflare's Turnstile CAPTCHA for form validation. It supports both implicit and explicit rendering modes.
You can find the document of Cloudflare Turnstile here.
Installation
1. Composer Installation
Install the package via Composer:
composer require davodm/codeigniter4-turnstile
2. Configuration
After installation, configure your Cloudflare Turnstile site and secret keys. You can do this by setting the environment variables in your .env file:
turnstile.siteKey=your-site-key turnstile.secretKey=your-secret-key
Usage
Rendering CAPTCHA
The library provides helper functions turnsitle to render the CAPTCHA in your views: turnstile_implicit(), turnstile_explicit(), and turnstile_explicit_render().
Implicit Mode
In your view file, you can render the Turnstile CAPTCHA in implicit mode by calling:
helper('turnstile'); echo turnstile_implicit('turnstile_field_name', 'auto', 'normal');
Explicit Mode
For explicit mode, you need to follow these steps:
- Register Fields: In your view file, register the fields where the CAPTCHA should appear:
helper('turnstile'); turnstile_explicit('turnstile_field_name', 'dark', 'normal');
- Create a Element: Manually create a element in your view with an id matching the fieldName parameter:
<div id="turnstile_field_name"></div>
- Render the CAPTCHA: Finally, render the CAPTCHA by calling turnstile_explicit_render() before the closing tag:
<?= turnstile_explicit_render(); ?>
Validation
To validate the CAPTCHA response, you can use the TurnstileRules validation rule in your controller or model.
Adding the Validation Rule
First, register the validation rule in your validation configuration file (app/Config/Validation.php):
public $ruleSets = [ // other rules \CI4CFTurnstile\Validation\TurnstileRules::class, ];
Then, in your form validation logic:
$validation = \Config\Services::validation(); $validation->setRules([ 'turnstile_field_name' => 'required|turnstile_verify', ]); if (!$validation->withRequest($this->request)->run()) { // Handle validation errors $errors = $validation->getErrors(); // Display errors to the user }
Example
Here’s a complete example of using CI4CFTurnstile in a controller:
namespace App\Controllers; use CodeIgniter\Controller; class FormController extends Controller { public function index() { helper('turnstile'); return view('form'); } public function submit() { $validation = \Config\Services::validation(); $validation->setRules([ 'turnstile_field_name' => 'required|turnstile_verify', ]); if (!$validation->withRequest($this->request)->run()) { return redirect()->back()->withInput()->with('errors', $validation->getErrors()); } // CAPTCHA passed // Proceed with form processing } }
In your form.php view file:
<!-- Implicit form model -->
<form method="post" action="/form/submit">
<!-- Other form fields -->
<?= turnstile_implicit('turnstile_field_name'); ?>
<button type="submit">Submit</button>
</form>
<!-- Explicit form model -->
<form method="post" action="/form/submit">
<!-- Other form fields -->
<div id="turnstile_field_name"></div>
<?= turnstile_explicit('turnstile_field_name', 'dark', 'normal'); ?>
<button type="submit">Submit</button>
</form>
<?=turnstile_explicit_render?>
</body>
License
This project is licensed under the MIT License - see the License file for details.
Author
Davod Mozafari - Twitter
davodm/codeigniter4-turnstile 适用场景与选型建议
davodm/codeigniter4-turnstile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.68k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 davodm/codeigniter4-turnstile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davodm/codeigniter4-turnstile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-18