我在Yii的程序文件夹(vendor/yiisoft/yii2)下建立了一个自己的文件夹(Roc)放自己的公用程序。
这个验证码类很轻便,只有一个程序文件 Captcha.php,还有一个字体文件,我也放在了同一个文件夹 Captcha下面
所以有两个文件:
vendor\yiisoft\yii2\Roc\Captcha\Captcha.php
vendor\yiisoft\yii2\Roc\Captcha\elephant.ttf
这是验证码的类。
<?php
namespace yii\Roc\Captcha;
use yii\base\Model;
use Yii;
//验证码类
class Captcha Extends Model {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen = 4; //验证码长度
private $width = 130; //宽度
private $height = 50; //高度
private $img; //图形资源句柄
private $font; //指定的字体
private $fontsize = 20; //指定字体大小
private $fontcolor; //指定字体颜色
//构造方法初始化
public
function __construct() {
$this->font = dirname(__FILE__) . '/elephant.ttf'; //注意字体路径要写对,否则显示不了图片
//echo $this->font;die();
}
//生成随机码
private
function createCode() {
$_len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codelen; $i++) {
$this->code.= $this->charset[mt_rand(0, $_len)];
}
}
//生成背景
private
function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
}
//生成文字
private
function createFont() {
$_x = $this->width / $this->codelen;
for ($i = 0; $i < $this->codelen; $i++) {
$this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
}
}
//生成线条、雪花
private
function createLine() {
//线条
for ($i = 0; $i < 6; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
//雪花
for ($i = 0; $i < 100; $i++) {
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
}
}
//输出
private
function outPut() {
//header('Content-type:image/png');
Yii::$app->response->headers->set('Content-Type', 'image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public
function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public
function getCode() {
return strtolower($this->code);
}
}
验证码要有一个方法才能在yii2环境下使用,我把生成验证码的方法放在了
UserController
里面,UserController文件里要添加对验证码类文件的引用:
use yii\Roc\Captcha\Captcha;
public function actionCaptcha(){
$captcha = new Captcha(); //实例化一个对象
$captcha->doimg();
Yii::$app->session->set('captcha', $captcha->getCode());//验证码保存到SESSION中
exit; // Added by Roc at 2018/7/22, without this, yii2 will send header twice, it whill throw a exception
}
在这个方法后面我加了一个 exit语句,因为如果不加,则yii会报一个有两个文件头的异常。
现在有类的原代码了,有对该类的引用了,也有执行该类的方法了,我们可以使用验证码了。
在要使用验证码的 View 文件里添加:
<img style="float:left;margin-left:130px;" title="点击刷新" src="<?= Url::toRoute('user/captcha') ?>" align="absbottom" onclick="this.src='<?= Url::toRoute('user/captcha') ?>'+'&'+Math.random();"></img>
即可生成验证码
在检查验证码的控制器里:
$loginForm = new LoginForm(['scenario' => 'register1']);
$post = Yii::$app->request->post();
if($loginForm->load($post)){
if(Yii::$app->session->get('captcha') <> $loginForm->verifyCode){
Yii::$app->session->setFlash('message', '验证码不正确');
return $this->render('register', [
'loginForm' => $loginForm,
]);
}
对从窗体上提交上来的验证码和存储在session里面的验证码做比对。
这个验证码程序在生成验证码的时候,保存在服务器的名为:captcha 的 session里面了。