Korzen
Bot Protection
In these docs

Laravel

Bot Protection in Laravel: the tag in the layout, the _kz cookie left unencrypted, trusted proxies, and Korzen's PHP package as the route's middleware.

This guide is for Laravel 11 and later, with Korzen's package for PHP, korzen/bot-protection: a middleware that asks /v1/decide, gives up at your timeout and lets the visitor through on any error. Two settings matter as much as the call: Laravel encrypts every cookie it reads unless told otherwise, and it ignores proxies until you trust them.

Payment, login and sign-up routes: turn on protectedAction: true, so a script that never loads your page cannot take the action. Protecting a checkout

The tag

In the layout your sign-up, sign-in and checkout pages use:

{{-- resources/views/layouts/app.blade.php, in <head> --}}
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>

For visitors whose network blocks Korzen's host, firstParty: true serves the tag and its beacon from your own domain. First-party mode

The call

Behind Cloudflare? A Worker can do this without changing your app. Cloudflare Workers

Install the package with Composer:

composer require korzen/bot-protection

In bootstrap/app.php, name the middleware, leave the tag's cookie unencrypted, and trust your proxy:

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
    $middleware->alias(['protect' => \Korzen\BotProtection\Laravel\Protect::class]);
    $middleware->encryptCookies(except: ['_kz']); // the tag's cookie is not Laravel's
    $middleware->trustProxies(at: '*');            // only if the app is reachable through your proxy alone
})

Read the key through the config, so it still works after php artisan config:cache:

// config/services.php
'korzen' => [
    'key' => env('KORZEN_API_KEY'),
],

Bind the client once, and Protect with its options. On a block it answers with your onBlock, here the form's own error:

// app/Providers/AppServiceProvider.php
use Korzen\BotProtection\Client;
use Korzen\BotProtection\Laravel\Protect;

public function register(): void
{
    // The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency
    $this->app->singleton(Client::class, fn () => new Client((string) config('services.korzen.key'), url: 'https://eu.korzen.dev', timeoutMs: 200));
    $this->app->bind(Protect::class, fn ($app) => new Protect(
        $app->make(Client::class),
        ip: fn ($request) => $request->ip(), // the visitor's, behind the proxies you trust
        protectedAction: true, // sign-up, login and payment: a script that never loaded your page holds no token; a page load spends none
        onBlock: fn ($request) => back()->withInput()->withErrors(['email' => 'We could not create your account.']),
    ));
}

Then put it on the route that creates the account; protect:/register names the action. With Breeze, in routes/auth.php:

// routes/auth.php (Breeze)
Route::post('register', [RegisteredUserController::class, 'store'])->middleware('protect:/register');
// app/Http/Controllers/Auth/RegisteredUserController.php (Breeze), in store()
$decision = $request->attributes->get('korzen.decision'); // a Decision, or null
$user = User::create([
    // ... your fields
    'korzen_request_id' => $decision?->requestId, // see "Apply the action"
]);

The package sends the request's headers in the order they came, as $_SERVER keeps them (it reverses PHP-FPM's, which lists them last first); behind nginx, a load balancer or Cloudflare, that is the order the last hop forwards them in. The (string) keeps the app starting when the key is unset: an empty key is answered 401 on every call, which lets the visitor through, as any failure does.

The visitor's IP

$request->ip() is your proxy's address until you trust it. trustProxies(at: '*') trusts whatever connects, which is right when the app can only be reached through your load balancer or Cloudflare; otherwise list your proxy's addresses. PHP-FPM opens a new connection for each request it serves, so each call carries a TLS handshake: from far from Europe, choose a timeout with room for it.

Apply the action

Protect applies action: on block it answers 403, or what your onBlock returns, and the controller never runs. Make that answer the error the form shows when it fails for any other reason, so a bot learns nothing. The account starts in monitor mode, where action is always allow: nothing changes for visitors until you turn on block mode.

Keep the answer's request_id with what the action created, such as the account or the order: it names that decision when you ask us about it. The answer

Test it on your machine

Run php artisan serve and open http://localhost:8000. Pages on localhost always accept the tag and are never listed as a domain. Locally the IP is 127.0.0.1 or ::1, and that is fine. In Docker on your machine, the app sees the bridge's address instead, such as 172.17.0.1, and that is fine too.

  1. Set KORZEN_API_KEY in .env and open the sign-up page. In the browser's developer tools, the _kz cookie appears after a few seconds.
  2. Sign up. The decision shows on the dashboard's Decisions page, under the path you sent.
  3. Check fail open: set a wrong key and sign up again. The call answers 401, and the sign-up goes through.

Common mistakes

  • _kz missing from encryptCookies(except: …). Laravel then fails to decrypt the tag's cookie, and no decision carries the tag's token.
  • env('KORZEN_API_KEY') in the provider. After config:cache, env() outside the config files returns null.
  • No trusted proxies behind a load balancer. Every visitor then has the balancer's IP.
  • Calling /v1/decide from the browser. The API key would reach every visitor; the call belongs on your server.
  • A line in your log that starts korzen: the visitor's IP sent to Bot Protection. The client writes it once, at the first decision whose IP is your own network's (a load balancer's, a proxy's, a container's), which Bot Protection judges as no IP: set ip to the visitor's address (the visitor's IP). In Docker on your machine it is the bridge's address, and harmless there.