php点击字切换验证码,Yii2下点击验证码的切换实例代码

最近需要用到验证码,搜索了很多关于Yii2验证码的切换的介绍,下面我来记录一下,有需要了解Yii2下验证码的切换的朋友可参考。希望此文章对各位有所帮助。

模型代码如下:

namespace app\admin\models;

use yii;

use yii\base\model;

use yii\captcha\Captcha;

class LoginForm extends Model{

public $verifyCode;

public $password;

public $username;

public function rules(){

return [

['verifyCode','captcha','captchaAction'=>'/admin/login/captcha','message'=>'{attribute}'],

[['password','username'],'required'],

];

}

}

控制器代码如下:

namespace app\admin\controllers;

use Yii;

use yii\web\Controller;

use app\admin\models\LoginForm;

use yii\filters\AccessControl;

use yii\filters\VerbFilter;

use yii\captcha\CaptchaAction;

class LoginController extends Controller{

public function actions(){

return [

'captcha'=>'yii\captcha\CaptchaAction',

'maxLength'=>4,

'minLength'=>3,

'width'=>10,

'height'=>10

];

}

public function actionIndex(){

$log = new LoginForm();

return $this->renderPartial("index",['model'=>$log]);

}

//授权规则

视图代码如下:

use yii\helpers\Html;

use yii\bootstrap\Nav;

use yii\bootstrap\NavBar;

use yii\widgets\Breadcrumbs;

use app\assets\AppAsset;

use yii\widgets\ActiveFiel;

use yii\widgets\ActiveForm;

use yii\captcha\Captcha;

use app\components\HelloWidget;

//url 创建

use yii\helpers\Url;

AppAsset::register($this);

?>

<?php $this->beginPage()?>

博客后台管理系统

=Html::cssFile('@web/css/bootstrap.min.css')?>

=Html::cssFile('@web/css/style.css')?>

=Html::jsFile("@web/js/jquery-1.11.3.min.js");?>

=Html::jsFile("@web/js/bootstrap.min.js");?>

<?php $this->beginBody()?>

//刷新验证码

function get_code(obj)

{

if(!obj)

{

obj = document.getElementById('code_img');

}

obj.src = obj.src + "&t="+Date.parse(new Date());

}

<?php $this->beginBody()?>

<?php $this->endPage()?>

这里整个代码差不多了但是要设置一样更重要的 app/vendor/yiisoft/yii2/captcha/的文件下的CaptchaAction.php这文件中修改getVerifyCode($regenerate = false) 的方法$regenerate参数为true (getVerifyCode($regenerate = true))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值