Download the PHP package eolica/laravel-cloudflare-turnstile without Composer

On this page you can find all versions of the php package eolica/laravel-cloudflare-turnstile. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-cloudflare-turnstile

Cloudflare Turnstile for Laravel

Laravel integration of eolica/cloudflare-turnstile.

Requirements

Installation

Install via Composer:

The service provider is registered automatically.

This package always requires the same version of eolica/cloudflare-turnstile, so update both together with --with-dependencies:

Configuration

Add your site key and secret key to the .env file:

Optionally, set the hostname the tokens must be issued for:

To customize the configuration, publish the configuration file:

Rendering the widget

Include Cloudflare's script once in your layout, for example in the <head>:

It also adds a preconnect hint so the browser connects to Cloudflare early. Any attribute, such as a nonce, is added to the <script> tag. Include it in the layout itself, not inside a Livewire component: the scripts Livewire adds to the page when it updates a component don't run.

Then add the widget inside your form:

The widget adds the token to the form in the cf-turnstile-response field.

Configure the widget with any of these attributes:

See Cloudflare's widget configuration for the values each one accepts. Any other attribute, such as class or data-callback, is added to the widget's container.

The widget is shown in your application's locale unless you set a language, and in English if Cloudflare doesn't support that language. Use language="auto" to show it in the visitor's browser language instead.

Livewire

Bind the widget to a property of your component with wire:model:

The widget works with wire:navigate and in components rendered after the page loads. It also works with form objects. If the page doesn't include Cloudflare's script, the widget logs an error in the browser's console.

A token can only be verified once, so the widget is reset after Cloudflare verifies its token, whether it passes or fails. The property returns to its initial value and the visitor gets a new token. You don't need to do anything for this.

If another field fails, the token isn't verified, so the visitor keeps it and can submit the form again without solving a new challenge.

seophp/head

If your application uses seophp/head-laravel, add Cloudflare's script and the preconnect hint to the <head> it renders with the TurnstilePlugin, instead of the script component:

Validation

Validate the token the widget adds to your form with the Turnstile rule:

The token is only checked once every other field passes, wherever the rule is placed. When another field fails, the rule doesn't send the token to Cloudflare and doesn't add an error, as the validation fails anyway. This avoids a request and keeps the token unused.

The rule fails when the token is missing, so it doesn't need the required rule.

It also sends the visitor's IP address to Cloudflare, taken from $request->ip(). If your application runs behind a proxy, such as Cloudflare itself or a load balancer, configure the trusted proxies so the IP address is the visitor's and not the proxy's. To stop sending it:

Or override the configuration for a single rule:

Pass the action configured in the widget to check that the token was issued for it:

If Cloudflare can't be reached, the validation fails and the exception is reported to your exception handler.

To customize the error message or translate it, publish the translations:

Or set a custom message for a single form with the turnstile key, as with any other rule:

Events

The rule dispatches a TokenVerified event with Cloudflare's response after each verification, whether it passes or fails. The visitor only sees a generic error, so listen to it to find out why a token was rejected:

$event->response is the VerifyResponse returned by the client and $event->action the action passed to the rule. The event is not dispatched when the field is missing or isn't a string, when another field fails, or when Cloudflare can't be reached and the exception is reported instead.

Using the client directly

Eolica\CloudflareTurnstile\TurnstileClient is registered in the container, configured with your secret key and expected hostname. Inject it wherever you need it:

See the core package documentation for everything TurnstileClient offers.

HTTP client

Requests to Cloudflare are sent through Laravel's HTTP client, so they fire its events, show up in tools like Telescope or Nightwatch, and use its global configuration. For example, to set a timeout for every outgoing request:

Testing

Fake Cloudflare's responses with Http::fake():

The rule compares the response with the expected values, so include the action you pass to the rule, and the hostname if you set TURNSTILE_HOSTNAME. Otherwise the rule fails.

Http::preventStrayRequests() also prevents real requests to Cloudflare.

To try the widget locally, use Cloudflare's testing keys, such as the site key 1x00000000000000000000AA and the secret key 1x0000000000000000000000000000000AA, which always pass. The hostname and the action are not checked with the test secret keys, so a rule with an action passes too.

License

The MIT License (MIT). See the license file for more information.


All versions of laravel-cloudflare-turnstile with dependencies

PHP Build Version
Package Version
Requires php Version ^8.5
eolica/cloudflare-turnstile Version self.version
guzzlehttp/psr7 Version ^2.4 || ^3.0
illuminate/config Version ^13.0
illuminate/container Version ^13.0
illuminate/contracts Version ^13.0
illuminate/http Version ^13.0
illuminate/support Version ^13.0
illuminate/validation Version ^13.0
illuminate/view Version ^13.0
psr/http-client Version ^1.0
psr/http-message Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package eolica/laravel-cloudflare-turnstile contains the following files

Loading the files please wait ...