PHP code example of jzaaa / cake-captcha

1. Go to this page and download the library: Download jzaaa/cake-captcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jzaaa / cake-captcha example snippets



return [
    'captcha' => [
        'route' => '/jzaaa/cake-captcha/',
        'config' => [
            'width' => 150,
            'height' => 52,
            'length' => 4,
            'charset' => '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ',
            'sensitive' => false,
            'sessionKey' => 'captcha',
            'bgColor' => '#f5f7fb',
            'textColor' => '#1f2937',
            'lineColor' => '#d6dde8',
            'applyEffects' => true,
            'applyNoise' => false,
            'distort' => false,
            'applyPostEffects' => false,
            'randomizeFonts' => false,
            'maxLinesBehind' => 1,
            'maxLinesFront' => 0,
        ],
    ],
];


$captchaUrl = $this->Url->build([
    'plugin' => 'JZaaa/CakeCaptcha',
    'controller' => 'Captcha',
    'action' => 'index',
]);

use JZaaa\CakeCaptcha\Captcha;

$userCode = $this->request->getData('userCode');
$captcha = new Captcha([
    'session' => $this->request->getSession(),
]);

if (!is_string($userCode) || $userCode === '' || !$captcha->check($userCode)) {
    // 返回验证码错误,停止处理本次表单。
} else {
    // 验证成功,继续处理表单;验证码已从 Session 删除。
}

$captcha = new Captcha([
    'session' => $this->request->getSession(),
    'width' => 180,
    'height' => 60,
]);
$captcha->create();

return $this->response
    ->withType('jpeg')
    ->withHeader('Cache-Control', 'no-store')
    ->withStringBody($captcha->image());