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();">