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);
- reCAPTCHA script loading
- grecaptcha.ready() fired, requesting tokens for both examples/v3strict and examples/v3immutable
- Strict verifier (threshold: 0.99, action: examples/v3strict) response:
Loading...
- Standard verifier (threshold: 0.5, action: examples/v3immutable, derived from the same $baseRecaptcha instance) response:
Loading...
⤴️ Try again