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


// src/Application.php
public function bootstrap()
{
    parent::bootstrap();
    $this->addPlugin('JZaaa/CakeCaptcha', ['routes' => true, 'bootstrap' => true]);
}


return [
    'captcha' => [
        'route' => '/jzaaa/cake-captcha/', // 默认访问路由
        'config' => [
            'width' => 150, // 验证码图像宽
            'height' => 40, // 验证码图像高
            'sensitive' => false, // 是否对大小写敏感
            'sessionKey' => 'captcha', // 存储session key
            'length' => 4, // 验证码长度
            'charset' => '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ', // 验证码字符集
            'applyPostEffects' => true, // 是否应用后期效果
        ]
    ]
];


 $captcha = $this->Url->build('/jzaaa/cake-captcha')

use JZaaa\CakeCaptcha\Captcha;

// 检测验证码是否合法
public function check()
{
    if ($this->request->is('post')) {
        $userCode = $this->request->getData('userCode');

        if (!empty($userCode)) {
            $captcha = new Captcha([
                'session' => $this->request->getSession()
            ]);
            if ($captcha->check($userCode)) {
                // valid
            }
        } else {
                // invalid
        }

    }
}