最近需要用到验证码,搜索了很多关于Yii2验证码的切换的介绍,下面我来记录一下,有需要了解Yii2下验证码的切换的朋友可参考。希望此文章对各位有所帮助。
模型代码如下:
rush:PHP;">
'/admin/login/captcha','message'=>'{attribute}'],[['password','username'],'required'],];
}
}
控制器代码如下:
rush:PHP;">
public function actions(){
return [
'captcha'=>'yii\captcha\CaptchaAction','maxLength'=>4,'minLength'=>3,'width'=>10,'height'=>10
];
}
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]);
}
//授权规则
}
视图代码如下:
rush:PHP;">
beginPage()?>
博客后台管理系统
<Meta charset="utf-8">
=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");?>
beginBody()?>
Box">
beginBody()?>
</div>
<h4 class="login-title"><a href="https://www.jb51.cc/tag/boke/" target="_blank" class="keywords">博客</a><a href="https://www.jb51.cc/tag/houtai/" target="_blank" class="keywords">后台</a>管理系统</h4>
<?php $form=ActiveForm::begin(['method'=>'post','action'=> \Yii::$app->urlManager->createUrl('/admin/login/index')])?>
<div class="login-input-<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> mb10">
<input type="text" class="form-control" name="username" placeholder="<a href="https://www.jb51.cc/tag/yonghuming/" target="_blank" class="keywords">用户名</a>">
</div>
<div class="login-input-<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> mb10">
<input class="form-control" type="password" name="password" placeholder="密码">
</div>
<div class="login-input-<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> mb30">
<input class="form-control" type="text" name="code" placeholder="验证码">
<span class="login-code">
<img id="code_img" src="<?= Url::toRoute('/admin/login/captcha')?>" title="点击刷新验证码" onClick="get_code(this);">
</span>
</div>
<input class="inputbtn bg-success btn-block" type="submit" value="登 录">
<?php ActiveForm::end();?>
<?php $this->endPage()?>
这里整个代码差不多了但是要设置一样更重要的 app/vendor/yiisoft/yii2/captcha/的文件下的CaptchaAction.PHP这文件中修改getVerifyCode($regenerate = false) 的方法$regenerate参数为true (getVerifyCode($regenerate = true))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。