yii\captcha\CaptchaAction::validate PHP Method

validate() public method

Validates the input to see if it matches the generated code.
public validate ( string $input, boolean $caseSensitive ) : boolean
$input string user input
$caseSensitive boolean whether the comparison should be case-sensitive
return boolean whether the input is valid
    public function validate($input, $caseSensitive)
    {
        $code = $this->getVerifyCode();
        $valid = $caseSensitive ? $input === $code : strcasecmp($input, $code) === 0;
        $session = Yii::$app->getSession();
        $session->open();
        $name = $this->getSessionKey() . 'count';
        $session[$name] = $session[$name] + 1;
        if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
            $this->getVerifyCode(true);
        }
        return $valid;
    }

Usage Example

Example #1
0
 public function validate($input, $caseSensitive)
 {
     // Skip validation on AJAX requests, as it expires the captcha.
     if (Yii::$app->request->isAjax) {
         return true;
     }
     return parent::validate($input, $caseSensitive);
 }