yii验证码

1.基本概念
1.1定义验证码action

public function actions()
{
	return [
		//验证码action
		'captcha' => [
		'class' => 'yii\captcha\CaptchaAction',
		'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null,
		'backColor'=>0x000000,//背景颜色
		'maxLength' => 5, //最大显示个数
		'minLength' => 4,//最少显示个数
		'padding' => 3,//间距
		'height'=>34,//高度
		'width' => 90,  //宽度
		'foreColor'=>0xffffff,     //字体颜色
		'offset'=>4        //设置字符偏移量 有效果
		],                
	];
}

1.2视图中输出验证码图片

<?php 
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'verifyCode')->widget('yii\captcha\Captcha', [
'captchaAction'=>'test/captcha',	
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('验证一下', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

1.3模型层验证

public function rules() 
{ 
	return [ 
		[['name', 'email', 'subject', 'body'], 'required'], 
		['email', 'email'], 
		['verifyCode', 'captcha']   
	]
}

1.4网页刷新验证码不刷新问题

<?php
namespace frontend\components;
use yii\web\Response;

class CaptchaAction extends \yii\captcha\CaptchaAction {
 
	 public function run() { 
        $this->setHttpHeaders();
        \Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->renderImage($this->getVerifyCode(true));	
	 }

}
?>

2.使用
2.1控制器

<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;

/**
 * Site controller
 */
class TestController extends Controller {


    public function actions() {
        return [
            //验证码action
            'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null,
            'backColor'=>0x000000,//背景颜色
            'maxLength' => 5, //最大显示个数
            'minLength' => 2,//最少显示个数
            'padding' => 3,//间距
            'height'=>34,//高度
            'width' => 90,  //宽度
            'foreColor'=>0xffffff,     //字体颜色
            'offset'=>4        //设置字符偏移量 有效果
            ],                
        ];
    }
    public function actionReg() {
        $model = new \backend\models\RegForm();
        return $this->render('reg',['model'=>$model]);
    }
}

2.2模型

<?php
namespace backend\models;

use Yii;
use yii\base\Model;


class RegForm extends Model {

    public $verifyCode;

    public function rules() {
        return [ 
            ['verifyCode', 'captcha','captchaAction'=>'test/captcha']   //这里的test/captcha表示那个控制器下使用就写相应的控制器信息,这里是在text/reg中使用。验证码调用的是captcha
        ];
    }
}

2.3视图

<?php 
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'verifyCode')->widget('yii\captcha\Captcha', [
'captchaAction'=>'test/captcha',	
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('验证一下', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原克技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值