yii2验证码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zchqq/article/details/82842044

模型文件

public function rules()
{
    return [
        [['track_id', 'track_name', 'track_content', 'src', 'add_time'], 'required'],
        [['track_id', 'add_time'], 'integer'],
        [['track_content'], 'string'],
        [['track_name'], 'string', 'max' => 108],
        [['src'], 'string', 'max' => 152],
        [
            'verifyCode',
            'captcha',
            'captchaAction'=>'comment/default/captcha'
        ]
    ];
}

控制器文件

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

视图文件

<?php
use yii\captcha\Captcha;
?>
<form role="form">
    <fieldset>
        <div class="form-group">
            <input class="form-control" placeholder="请输入用户名" name="username" type="text" autofocus>
        </div>
        <div class="form-group">
            <input class="form-control" placeholder="请输入密码" name="password" type="password">
        </div>
        <div class="form-group">
            <input class="form-control" style="width: 226px; display: inline-block;" placeholder="请输入验证码" name="captcha" type="text">
            <?=Captcha::widget(['name'=>'captcha-img',
                'captchaAction'=>'stock/captcha',
                'imageOptions'=>['id'=>'captcha-img', 'title'=>'换一个', 'style'=>'cursor:pointer;'],
                'template'=>'{image}']);?>
        </div>
        <div class="checkbox">
            <label>
                <input name="remember" type="checkbox" value="1">记住我
            </label>
        </div>
        <a href="" class="btn btn-lg btn-success btn-block">登录</a>
    </fieldset>
</form>

猜你喜欢

转载自blog.csdn.net/zchqq/article/details/82842044