Anti-bot checks (captcha drivers)

The forms of a site are checked against bots by one of several drivers: the built-in image captcha (the default, unchanged), or a modern provider: Cloudflare Turnstile, Google reCAPTCHA (v3 score or v2 checkbox), hCaptcha, Friendly Captcha or the self-hosted ALTCHA proof of work. Cheap invisible layers (a honeypot field, a minimum fill time, a single-use form token, a limit of failed checks per IP) work with any driver. Nothing changes until a site turns something on. The keys, the admin page and operations are on the admin page of the anti-bot drivers.

In a form

A form2 form with ->captcha() and the captcha rule draws and checks the driver of that form; the code does not change when the driver does:

form($_POST)
    ->validate(['email' => 'trim|required|valid_email', 'captcha' => 'captcha[contact]'])
    ->email()
    ->captcha('captcha', '', ['captcha_form' => 'contact'])
    ->save();

The form name (contact above) picks the driver of the form. Without it the name is the module of the request (register, comments). A module's own form uses $this->_captcha_block() and $this->_captcha_check() as before; a form outside form2 calls the API:

$html = _class('captcha')->render('contact');               // the widget, inside the <form>
$ok = _class('captcha')->verify('contact');                 // checks $_POST; details in _class('captcha')->_last
$csp = _class('captcha')->csp('contact');                   // directive => sources the widget needs

An empty captcha always fails the form (before 2026-11-28 a captcha rule without required let an empty answer through).

Drivers

DriverWhat the visitor seesWhere the data goesCost
defaultDigits on a noisy image, typed into a field.Nowhere: the answer is kept in the session. Free. Weak: OCR and vision models read it.
turnstileUsually nothing or one click (managed, non-interactive or invisible widget).Cloudflare. Free (20 widgets per account), Enterprise on request.
recaptchav3: nothing, a score per request; v2: the "I'm not a robot" box and image puzzles.Google (Google cookies).10,000 checks a month free per organization, then paid (Google Cloud).
hcaptchaA checkbox, sometimes image puzzles; invisible on paid plans.hCaptcha (US). Free plan; Pro and Enterprise paid.
friendlycaptchaA small widget that solves a puzzle by itself.Friendly Captcha (Germany; EU endpoint on paid plans).Free for small non-commercial sites, then paid.
altchaA checkbox; the browser works for a moment (proof of work).Nowhere: made and checked by the site.Free, open source (MIT).

ALTCHA needs HTTPS (or localhost): browsers run its proof of work only in a secure context. It is a speed bump, not a wall: it makes each submission cost a second or two of CPU, which stops mass spam, but a determined bot solves it like a browser does; pair it with the layers below. reCAPTCHA v3 asks Google for a token when the form is sent, so the form needs JavaScript.

Fallback and fail policy

Each form has a chain: its driver, then the fallback drivers. A driver without keys is skipped; a provider that keeps failing (three errors in five minutes) is paused and new forms get the next driver; the image captcha is the last resort, so a form is never left unchecked by a configuration mistake. When the provider cannot be asked at the moment of the check (timeout, error), the fail policy decides: closed (default) refuses the form, open lets it through. Open applies only when the provider cannot be reached (no answer, a 5xx error), never to a 429 or a refused key; each such pass is logged and one IP gets a few of them at most. A good chain: turnstile, then altcha, closed (new installs start with it).

Invisible layers

  • Honeypot: a hidden text field people leave empty; a bot that fills every field is refused.
  • Minimum fill time: a form sent a second after it was drawn is refused (a signed time in the form).
  • Single-use form token: each drawn form is accepted once, so a captured POST cannot be replayed.
  • Failed checks per IP: after many failed checks an address is refused for a while without asking the provider.

Each layer is off by default and works with any driver, the image captcha included. The single-use parts (ALTCHA answers, the form token) are remembered in the cache or the database; a page served from a full-page cache gives every visitor the same token and challenge, so only the first one passes: do not cache pages with these forms. The minimum fill time, the form token and ALTCHA need the site key YF_SECRET_KEY.

Captcha after failed attempts

The user login and the lost password form can stay without a captcha until an IP address or an account failed a few times (wrong password, unknown login or e-mail); then they show the widget of their driver until the hour is over. Off by default. A site's own form, a contact form for example, uses the same gate:

$captcha = _class('captcha');
if ($_POST && ! $captcha->gate_ok('contact', $_POST['email'] ?? '')) return $this->_refuse();
// on a refused or suspicious message: $captcha->gate_fail('contact', $_POST['email'] ?? '');
$html = $captcha->gate_html('contact');                     // '' until the gate is needed

Image captcha hardening

The built-in image captcha keeps its look and its answer by default. Switches make it stricter: a code lifetime, a code bound to the form it was drawn for, a limit of wrong codes per session, a stronger distortion. They help against simple scripts only; vision models read distorted digits, so a site that gets bot traffic should choose another driver.

Content-Security-Policy

DriverSources
turnstilescript-src and frame-src https://challenges.cloudflare.com
recaptchascript-src https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; frame-src https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ (www.recaptcha.net instead of www.google.com with SCRIPT_HOST)
hcaptchascript-src, frame-src, style-src, connect-src https://hcaptcha.com https://*.hcaptcha.com
friendlycaptchascript-src https://cdn.jsdelivr.net/npm/@friendlycaptcha/; connect-src https://global.frcapi.com (or https://eu.frcapi.com); worker-src blob:
altchaworker-src blob:; the widget script from the site (assets.VENDOR_LOCAL) or https://cdn.jsdelivr.net
defaultimg-src 'self'

reCAPTCHA v3 and the image captcha also use a small inline script; a site that sends a nonce-based policy sets _class('captcha')->CSP_NONCE for the request and the reCAPTCHA script carries it. The Friendly Captcha script is loaded with its integrity hash (SRI). _class('captcha')->csp() returns the sources of the configured drivers.