在yii2.0中封装一个生成验证码的控制器
frontend目录下/封装的验证码类: <?php
namespace frontend\controllers; use yii\base\Controller; class CapathController extends Controller {
;;;;;
;,,), mt_rand(,), mt_rand(,));
imagefilledrectangle(,,;,),mt_rand(,),mt_rand(,));
imagettftext(,),,),;;,),mt_rand(,),mt_rand(,));
imageline(,,,,;;,),mt_rand(,),mt_rand(,));
imagestring(,),mt_rand(,,$this->height),'*',$color);
}
}
//输出
private function outPut() {
header('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);
}
}
?>
控制器下调用:
<?php
use frontend\controllers\CapathController;
//定义生成验证码方法 public function actionCreatecapath() { //开启session \Yii::$app->session->open(); //输出图像 $capthObj = new CapathController(); $capthObj->doimg(); //存验证码 $_SESSION['capath'] = $capthObj->getCode();//获取验证码保存到SESSION中 }
?>
视图层:
<px;cursor:pointer;" onclick="this.src='?r=上面控制器/createcapath&'+Math.random();">
最新文章
- JavaScript中的变量及数据类型
- [Unity] 精灵动画制作中需要注意的一些问题
- Cygwin的安装与配置
- 1.No MBR错误
- SplendidCRM 如何添加及使用中文语言包
- elementoryOS / ubuntu U盘启动问题的解决
- 在集群环境中使用 EhCache 缓存系统|RMI 集群模式
- Buffer -nodejs
- PV模型
- poj 2250 Compromise(区间dp)
- HTML5总结
- 关于HashSet集合add元素
- go 语言图片像素点处理
- XXS level9
- Apache POI导出excel表格
- 卡通渲染Shader
- [No0000187]可能是把Java内存区域讲的最清楚的一篇文章
- ES6学习笔记四:Proxy与Reflect
- Shell if else
- 【性能测试】:解决loadrunner执行脚本启动慢的问题