cleup / captcha
基本验证码
v1.0.2
2024-02-12 18:07 UTC
This package is auto-updated.
Last update: 2024-09-08 05:32:05 UTC
README
为项目安全提供基本验证码
安装
使用composer
composer require cleup/captcha
使用方法
创建图片
创建验证码图片并将其放置在控制器文件或应用程序的单独页面中
use Cleup\Package\Captcha\Image; # Instance of the class $captcha = new Image([ # Captcha Parameters 'width' => 300, 'height' => 60, /* You can set a range for these parameters using various methods (rand, random_int) */ // 'length' => 5, // 'fontSize' => 24, // 'allowedCharacters' => '1234567890abcdefghijkmnpqrstuvwxyz', // 'width' => 140, // 'height' => 60, // 'maxLines' => 8, // 'minLines' => 4, // 'pointColor' => array(77, 77, 77), // 'textColor' => array( // rand(0, 78), // rand(0, 100), // rand(0, 7) // ) ]); // Create a captcha image $captcha->create(); /* The end of the file. */
在HTML页面上输出数据
... <img src="/image.php" alt="Captcha"/> // Or <img src="/captcha/image.php?<?= rand(10, 1000); ?>" alt="Captcha"/> ...
代码验证
代码验证使用会话。请确保已启动会话并可用于记录
use Cleup\Package\Captcha\Verification; # Instance of the class $verification = new Verification(); // Raw data $data = json_decode( file_get_contents('php://input'), true ); // Getting the code $code = $data['code'] ?? false; if (!$code) { $response = [ 'success' => false, 'message' => 'You have not entered the required data' ]; } else { if ($verification->verify($code)) { $response = [ 'success' => true, 'message' => 'The code is correct' ]; } else { $response = [ 'success' => false, 'message' => 'The code is incorrect' ]; } } header('Content-type: application/json'); echo json_encode($response);
如果您遇到任何困难,请研究位于demo
文件夹中的项目演示版本