Skip to content
This is the documentation for pieceofcake2/captcha, a CakePHP 2.x plugin for pieceofcake2/cakephp. About this fork.

Verifying the Token (CaptchaComponent) ​

php
public $components = [
    'Captcha.Captcha' => [
        'actions' => ['add', 'login'],
    ],
];

Automatic verification ​

For the actions in actions (['*'] for all actions), the component verifies the token in startup(), after beforeFilter() and before the action. Only the methods in methods are verified, so the action can still show the form on GET.

SettingDefaultDescription
actions[]Actions verified automatically. ['*'] for all actions.
methods['post', 'put', 'patch']HTTP methods verified automatically.
onFailureredirectredirect: set a flash message and redirect. exception: throw exception. none: only record the result.
flashCAPTCHA verification failed. Please try again.Flash message for redirect. false for none.
flashOptions[]Options for FlashComponent::set(), e.g. ['element' => 'error'].
redirectrefererRedirect URL for redirect.
exceptionForbiddenExceptionException class for exception, e.g. BadRequestException.
trustProxyfalseRead the client IP (and the host for 'hostname' => true) from the proxy headers. See Security Notes.

Any other setting is provider configuration and overrides Configure::read('Captcha').

redirect discards the submitted data. To redisplay the form with the data, use 'onFailure' => 'none' and check the result in the action:

php
public function add()
{
    if ($this->request->is('post')) {
        if (!$this->Captcha->verify()) {
            $this->Contact->invalidate('captcha', __('Please complete the CAPTCHA.'));
        } elseif ($this->Contact->save($this->request->data)) {
            return $this->redirect(['action' => 'index']);
        }
    }
}

verify() reuses the result of the automatic verification, so the token is sent to the provider only once (tokens are single use).

Manual verification ​

php
if (!$this->Captcha->verify()) {
    $errors = $this->Captcha->getErrors(); // e.g. ['timeout-or-duplicate']
}
MethodDescription
verify(?string $token = null, array $settings = []): boolVerify the token. The token defaults to the provider's field in the request data. $settings overrides the configuration for this call, e.g. ['action' => 'login'].
getErrors(): arrayError codes of the last verification.
getResult(): ?CaptchaResultThe last result: isSuccess(), getErrors(), getHostname(), getAction(), getScore(), getData($key) (the decoded verify response).
token(): ?stringThe token in the request data.
requiresVerification(): boolWhether the current request is verified automatically.
provider(array $settings = [])The configured provider.
setHttpSocket(?HttpSocket $http)Use this HttpSocket for the verify requests (e.g. a mock in tests).

Verifying outside a controller ​

php
App::uses('CaptchaFactory', 'Captcha.Lib');
App::uses('CaptchaVerifier', 'Captcha.Lib');

$verifier = new CaptchaVerifier(CaptchaFactory::provider(['action' => 'login']));
$result = $verifier->verify($token, $clientIp);

Error Codes ​

getErrors() returns the provider's error-codes (e.g. invalid-input-response, timeout-or-duplicate, invalid-input-secret, hCaptcha's already-seen-response) and the codes added by the plugin:

CodeDescription
missing-input-responseNo token was submitted. No request is sent.
invalid-input-responseThe token is longer than the provider allows. No request is sent.
missing-input-secretsecretKey is not configured. No request is sent and a warning is logged.
verification-unavailableNetwork error, timeout or non-2xx response from the verify endpoint.
invalid-jsonThe verify endpoint returned something other than a JSON object.
unknown-errorsuccess was not true and there were no error codes.
hostname-mismatchThe hostname in the response does not match hostname.
action-mismatchThe action in the response does not match action.
score-too-lowreCAPTCHA v3 score is missing or below scoreThreshold.

See LICENSE for copyright and license information. Not affiliated with the Cake Software Foundation.