$first) { [$first, $second] = [$second, $first]; } $question = $isSubtraction ? "{$first} - {$second}" : "{$first} + {$second}"; $answer = (string) ($isSubtraction ? $first - $second : $first + $second); $request->session()->put("captcha.{$context}.answer", $answer); return $question; } protected function captchaIsValid(Request $request, string $context, string $field = 'captcha'): bool { $expected = (string) $request->session()->get("captcha.{$context}.answer", ''); $provided = trim((string) $request->input($field, '')); return $expected !== '' && hash_equals($expected, $provided); } protected function clearCaptcha(Request $request, string $context): void { $request->session()->forget("captcha.{$context}.answer"); } }