| Package Data | |
|---|---|
| Maintainer Username: | derekcodes-io |
| Maintainer Contact: | derek@derekcodes.io (Derek Codes) |
| Package Create Date: | 2022-12-19 |
| Package Last Update: | 2025-03-02 |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2026-01-06 15:00:03 |
| Package Statistics | |
|---|---|
| Total Downloads: | 101,077 |
| Monthly Downloads: | 5,809 |
| Daily Downloads: | 258 |
| Total Stars: | 57 |
| Total Watchers: | 2 |
| Total Forks: | 8 |
| Total Open Issues: | 0 |
A Laravel package to facilitate the server side validation of Cloudflare's Turnstile captcha service.
First you'll need an account Cloudflare and Turnstile setup for your website.
https://developers.cloudflare.com/turnstile/
Install via composer
composer require derekcodes/turnstile-laravel
Adding your secret key in the .env file
TURNSTILE_SITE_KEY="0x4AAAAAAAXXXXXXXXXXXXXX"
TURNSTILE_SECRET_KEY="0x4AAAAAAAXXXXXXXXXXXXXX"
Create a config/turnstile.php
php artisan vendor:publish --tag=turnstile-config
Be sure to add the front-end JavaScript from Turnstile: https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/
To save you some time, here's the Turnstile JavaScript necessary for an HTML form. Note that I assume you're using a Blade template, thus the {{ config('turnstile.site_key') }}.
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" defer></script>
window.onloadTurnstileCallback = function () {
turnstile.render('#your-form-id', {
sitekey: '{{ config('turnstile.site_key') }}',
callback: function(token) {
console.log(`Challenge Success ${token}`);
},
});
};
Add the use statement at the top of your file
use DerekCodes\TurnstileLaravel\TurnstileLaravel;
Initiate the TursileLaravel object and call the validate method passing the client response Turnstile's JavaScript provides
$turnstile = new TurnstileLaravel;
$response = $turnstile->validate($request->get('cf-turnstile-response'));
Ensure the response is valid
if (get_data($response, 'status', 0) == 1) {
// TODO: add success code here
}
Example success response
{
"status": 1
}
Example error response
{
"status": 0,
"turnstile_response": {
"success": false,
"error-codes": [
"invalid-input-response"
],
"messages": []
}
}
~ Derek Codes
Derek Codes is a comprehensive collection of tutorials on the most popular languages in the industry. You’ll find videos, guides and downloadable assets to help you learn to code PHP, Laravel, JavaScript, CSS and more. Thanks for watching!