Skip to content

Latest commit

 

History

History
98 lines (69 loc) · 2.27 KB

README.md

File metadata and controls

98 lines (69 loc) · 2.27 KB

Turnstile for Laravel

Latest Version Total Downloads Software License

A Laravel package that integrates Cloudflare's Turnstile CAPTCHA service.

Requirements

For Laravel integration:

  • PHP 8.2 or higher
  • Laravel 9 or higher

For standalone usage:

Installation

composer require teampanfu/laravel-turnstile

Laravel Integration

  1. Add to your .env:
TURNSTILE_SITEKEY=1x00000000000000000000AA
TURNSTILE_SECRET=1x0000000000000000000000000000000AA

Add the Widget

<form method="POST">
    @csrf
    <div class="cf-turnstile" data-sitekey="{{ config('turnstile.sitekey') }}"></div>
    <button type="submit">Submit</button>
</form>

<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>

See available configurations for theme, language, etc.

Validate the Response

$request->validate([
    'cf-turnstile-response' => ['turnstile'],
]);

Custom Error Message

In lang/[lang]/validation.php:

'custom' => [
    'cf-turnstile-response' => [
        'turnstile' => 'Please verify that you are human.',
    ],
],

Standalone Usage

The package can also be used without Laravel:

<?php

use Panfu\Laravel\Turnstile\Turnstile;

$turnstile = new Turnstile('your-secret-key');

try {
    if ($turnstile->validate($_POST['cf-turnstile-response'])) {
        // Verification successful
    }
} catch (\RuntimeException $e) {
    // Handle validation error (e.g., invalid-input-response)
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle network/request error
}

Testing

./vendor/bin/phpunit

License

MIT License