bugcat /
支持中文的PHP验证码
v1.0.0
2019-03-10 08:18 UTC
Requires
- php: >=7.0
- ext-gd: *
- intervention/image: ~2.2
This package is auto-updated.
Last update: 2024-09-20 21:52:43 UTC
README
安装
可以通过Composer安装Captcha服务提供商,在项目中配置composer.json文件,添加bugcat/captcha包并设置minimum-stability为dev。
{
"require": {
"bugcat/captcha": "~0.1"
},
"minimum-stability": "dev"
}
或者
使用Composer添加此包
composer require bugcat/captcha
使用composer update更新包或使用composer install安装。
在Windows系统中,需要在php.ini中包含GD2 DLL文件php_gd2.dll。此外,还需要包含php_fileinfo.dll和php_mbstring.dll以符合bugcat/captcha的依赖要求。
使用方法
要使用Captcha服务提供商,需要存储验证码字符并验证用户输入的正确性。此验证码包仅负责生成字符和绘制验证码图片。
首先使用它。
use Bugcat\Captcha\Captcha;
然后加载它。
$captcha = new Captcha();
使用自定义配置生成新的验证码,获取验证码代码,并存储它。
$config = []; //custom configuration $codes = $captcha->gnrt($config); $_SESSION['captcha'] = $codes;
将图片输出给用户
$captcha->img();
最后,验证用户输入
if ( $_POST['captcha'] == $_SESSION['captcha'] ) { //do something. } else { //do something. }
配置
在验证码包中提供了一个简单的配置示例,如下所示。
$captcha = new Captcha(); var_dump($captcha::CFG_EXP);
这是完整的描述。
$config = [ //Whether need a border, 1 is need and 0 is not, default 1. 'border' => '1|0', //The verification code length, 1 at least, default 4. 'charnum' => '4', //The fontsize of captcha, using px, 8 at least, default 16. 'fontsize' => '16', //The code direction, 0 is horizontal and 1 is vertical, default 0. 'codedir' => '1|0', //The font of code, use existing list or custom, default simhei. //This package provides these fonts: simhei, ABeeZee_regular, Asap_700, Khand_500, Open_Sans_regular, Roboto_regular, Ubuntu_regular, ygyxsziti2.0 . //If use custom, it needs the full and usable path. 'font' => 'simhei|Roboto_regular|/var/www/html/test/fonts/custom.ttf', //The character pool for captcha, use existing list or custom, default CHS. //This package provides these characters: // CHS : Chinese Simplified // CHT : Chinese Traditional // ENG : 2346789ABCDEFGHJMNPQRTUXYZ 'text' => 'CHS|CHT|ENG|custom string allow spaces because it will be ignored', ];
示例使用
use Bugcat\Captcha\Captcha; class Auth { private $captcha; public function __construct() { $this->captcha = new Captcha(); } public function vcode() { $config = ['charnum' => 2, 'text' => 'CHT']; $codes = $this->captcha->gnrt($config); $_SESSION['captcha'] = $codes; $this->captcha->img(); } public function register() { if ( $_POST['CSRF'] ) { if ( !$this->verify() ) { die( 'the verification code is error.' ); } //do something. } //do something. } private function verify() { if ( empty($_POST['captcha']) || empty($_SESSION['captcha']) ) { return false; } if ( $_POST['captcha'] == $_SESSION['captcha'] ) { return true; } else { retrun false; } } }