PHP code example of eolica / laravel-cloudflare-turnstile
1. Go to this page and download the library: Download eolica/laravel-cloudflare-turnstile library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
eolica / laravel-cloudflare-turnstile example snippets
use Eolica\LaravelCloudflareTurnstile\Rules\Turnstile;
use Livewire\Component;
class Contact extends Component
{
public string $email = '';
public ?string $captcha = null;
public function send(): void
{
$this->validate([
'email' => ['
use Eolica\LaravelCloudflareTurnstile\Head\TurnstilePlugin;
protected function configure(Head $head): void
{
$head->plugins([
TurnstilePlugin::make(),
]);
}
use Eolica\LaravelCloudflareTurnstile\Rules\Turnstile;
$request->validate([
'cf-turnstile-response' => [new Turnstile()],
]);
new Turnstile(sendRemoteIp: false)
new Turnstile(action: 'login')
$request->validate(
['cf-turnstile-response' => [new Turnstile()]],
['cf-turnstile-response.turnstile' => 'Please confirm you are human.'],
);
use Eolica\LaravelCloudflareTurnstile\Events\TokenVerified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
Event::listen(function (TokenVerified $event): void {
if (! $event->response->success) {
Log::debug('Turnstile rejected a token.', [
'action' => $event->action,
'errors' => $event->response->errorCodes,
]);
}
});
use Eolica\CloudflareTurnstile\TurnstileClient;
public function store(Request $request, TurnstileClient $turnstile)
{
$response = $turnstile
->withExpectedAction('login')
->verify($request->input('cf-turnstile-response', ''), remoteIp: $request->ip());
if (!$response->success) {
// Reject the request
}
}
use Illuminate\Support\Facades\Http;
Http::globalOptions(['timeout' => 5]);
use Eolica\CloudflareTurnstile\TurnstileClient;
use Illuminate\Support\Facades\Http;
Http::fake([
TurnstileClient::SITEVERIFY_URL => Http::response(['success' => true, 'action' => 'contact']),
]);