PHPackages                             davodm/codeigniter4-turnstile - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. davodm/codeigniter4-turnstile

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

davodm/codeigniter4-turnstile
=============================

CodeIgniter 4 package for integrating Cloudflare Turnstile Captcha

v0.1.9(8mo ago)11.1k↓43.2%1[1 PRs](https://github.com/davodm/codeigniter4-turnstile/pulls)MITPHPPHP &gt;=7.4

Since Aug 18Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/davodm/codeigniter4-turnstile)[ Packagist](https://packagist.org/packages/davodm/codeigniter4-turnstile)[ RSS](/packages/davodm-codeigniter4-turnstile/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (12)Used By (0)

Cloudflare Turnstile for CodeIgniter 4
======================================

[](#cloudflare-turnstile-for-codeigniter-4)

[![Build Status](https://github.com/davodm/codeigniter4-turnstile/actions/workflows/main.yml/badge.svg)](https://github.com/davodm/codeigniter4-turnstile/actions/workflows/main.yml/badge.svg)[![Packagist Version](https://camo.githubusercontent.com/ede8951afc46da1078e409651e767be0564b402b5d3bffb74c7d2d13cfcfdcb8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)](https://camo.githubusercontent.com/ede8951afc46da1078e409651e767be0564b402b5d3bffb74c7d2d13cfcfdcb8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)[![License](https://camo.githubusercontent.com/bcb715cab97ca94c85211ac932dc1bc12f15cd8fce877111587f149c9f8733e8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)](https://camo.githubusercontent.com/bcb715cab97ca94c85211ac932dc1bc12f15cd8fce877111587f149c9f8733e8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)[![Downloads](https://camo.githubusercontent.com/052c7fed00f250a53e80c410f42b7927cc72091fcb17060f0ca2f9a854678375/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)](https://camo.githubusercontent.com/052c7fed00f250a53e80c410f42b7927cc72091fcb17060f0ca2f9a854678375/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461766f646d2f636f646569676e69746572342d7475726e7374696c65)[![GitHub stars](https://camo.githubusercontent.com/bc9da11b1fd7797f8094dbfb209d9044688f18d8544ff96250021c69083894e6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6461766f646d2f636f646569676e69746572342d7475726e7374696c653f7374796c653d736f6369616c)](https://camo.githubusercontent.com/bc9da11b1fd7797f8094dbfb209d9044688f18d8544ff96250021c69083894e6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6461766f646d2f636f646569676e69746572342d7475726e7374696c653f7374796c653d736f6369616c)

**CI4CFTurnstile** is a [CodeIgniter 4](https://github.com/codeigniter4/CodeIgniter4) 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](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/).

Installation
------------

[](#installation)

### 1. Composer Installation

[](#1-composer-installation)

Install the package via [Composer](https://getcomposer.org/):

```
composer require davodm/codeigniter4-turnstile
```

### 2. Configuration

[](#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
-----

[](#usage)

### Rendering CAPTCHA

[](#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

[](#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

[](#explicit-mode)

For explicit mode, you need to follow these steps:

1. **Register Fields**: In your view file, register the fields where the CAPTCHA should appear:

```
helper('turnstile');
turnstile_explicit('turnstile_field_name', 'dark', 'normal');
```

2. **Create a**  **Element**: Manually create a  element in your view with an id matching the fieldName parameter:

```

```

3. **Render the CAPTCHA**: Finally, render the CAPTCHA by calling turnstile\_explicit\_render() before the closing tag:

```

```

### Validation

[](#validation)

To validate the CAPTCHA response, you can use the TurnstileRules validation rule in your controller or model.

#### Adding the Validation Rule

[](#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
-------

[](#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:

```
