reCAPTCHA demo

Immutable with*() builders

Home

The withExpectedHostname(), withExpectedAction(), withScoreThreshold(), and withChallengeTimeout() methods clone the ReCaptcha instance instead of mutating it in place.

This allows a single base ReCaptcha service in a dependency injection container or persistent worker runtime to safely derive multiple route-specific verifiers without leaking state:

$baseRecaptcha = (new ReCaptcha($secret))
    ->withExpectedHostname($_SERVER['SERVER_NAME'])
    ->withChallengeTimeout(120);

$strictVerifier   = $baseRecaptcha->withExpectedAction('examples/v3strict')->withScoreThreshold(0.99);
$standardVerifier = $baseRecaptcha->withExpectedAction('examples/v3immutable')->withScoreThreshold(0.5);
  1. reCAPTCHA script loading

Try again