40 lines
930 B
PHP
Executable File
40 lines
930 B
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class CaptchaController extends Controller
|
|
{
|
|
/**
|
|
* Generate a new arithmetic captcha.
|
|
*/
|
|
public function generate(Request $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
$a = random_int(1, 20);
|
|
$b = random_int(1, 20);
|
|
$operations = ['+', '-', '*'];
|
|
$operation = $operations[array_rand($operations)];
|
|
|
|
if ($operation === '+') {
|
|
$answer = $a + $b;
|
|
} elseif ($operation === '-') {
|
|
$answer = $a - $b;
|
|
} else {
|
|
$answer = $a * $b;
|
|
}
|
|
|
|
$expression = "{$a} {$operation} {$b} = ?";
|
|
|
|
Session::put('captcha_answer', (string) $answer);
|
|
|
|
return response()->json([
|
|
'expression' => $expression,
|
|
]);
|
|
}
|
|
}
|