Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
cached valid state
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Nov 8, 2017
1 parent fedad36 commit c900ad0
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ReCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ReCaptchaValidator extends Validator
/** @var string */
public $uncheckedMessage;

/** @var boolean */
protected $isValid = false;

public function init()
{
parent::init();
Expand Down Expand Up @@ -94,17 +97,21 @@ protected function validateValue($value)
}
}

$request = self::SITE_VERIFY_URL . '?' . http_build_query([
'secret' => $this->secret,
'response' => $value,
'remoteip' => Yii::$app->request->userIP
]);
$response = $this->getResponse($request);
if (!isset($response['success'])) {
throw new Exception('Invalid recaptcha verify response.');
if (!$this->isValid) {
$request = self::SITE_VERIFY_URL . '?' . http_build_query([
'secret' => $this->secret,
'response' => $value,
'remoteip' => Yii::$app->request->userIP
]);
$response = $this->getResponse($request);
if (!isset($response['success'])) {
throw new Exception('Invalid recaptcha verify response.');
}

$this->isValid = (boolean)$response['success'];
}

return $response['success'] ? null : [$this->message, []];
return $this->isValid ? null : [$this->message, []];
}

/**
Expand Down

0 comments on commit c900ad0

Please sign in to comment.