Answer the question
In order to leave comments, you need to log in
How to make a global variable?
How can I make the value the same for all pages?
The class that generates the captcha value
class MathCaptcha
{
private $firstNumber;
private $secondNumber;
private $name = 'result_captcha';
public function __construct(int $firstNumber,int $secondNumber, string $name = null)
{
$this->firstNumber = $firstNumber;
$this->secondNumber = $secondNumber;
if(!empty($name)) $this->name = $name;
}
public function getResult(): string
{
$time = time();
$first = random_int(0,$this->firstNumber);
$second = random_int(0,$this->secondNumber);
if($time & 1){
$str = $first.'-'.$second;
$result = $first - $second;
}else{
$str = $first.'+'.$second;
$result = $first + $second;
}
session([$this->name => $result]);
return $str;
}
}
public function register()
{
View::composer(['*'], function ($view) {
$captcha_register = (new MathCaptcha(20, 9,'captcha_register'))->getResult(20, 9);
$view->with([
'captcha_register' => $captcha_register,
]);
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question