最近需要用到验证码,搜索了很多关于Yii2验证码的切换的介绍,下面我来记录一下,有需要了解Yii2下验证码的切换的朋友可参考。希望此文章对各位有所帮助。
模型代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
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'],
];
}
}
|
控制器代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php
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]);
}
//授权规则
}
|
视图代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
<?php
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()?>
<!DOCTYPE html>
<html>
<head>
<title>博客后台管理系统</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<?=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 ?>
</head>
<?php $this->beginBody()?>
<body>
<div class="login-box">
<div class="login">
<div class="login-icon">
</div>
<h4 class="login-title">博客后台管理系统</h4>
<?php $form=ActiveForm::begin(['method'=>'post','action'=> \\Yii::$app->urlManager->createUrl('/admin/login/index')])?>
<div class="login-input-box mb10">
<input type="text" class="form-control" name="username" placeholder="用户名">
</div>
<div class="login-input-box mb10">
<input class="form-control" type="password" name="password" placeholder="密码">
</div>
<div class="login-input-box 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();?>
</div>
</div>
<script type="text/javascript">
//刷新验证码
function get_code(obj)
{
if(!obj)
{
obj = document.getElementById('code_img');
}
obj.src = obj.src + "&t="+Date.parse(new Date());
}
</script>
</body>
<?php $this->beginBody()?>
</html>
<?php $this->endPage()?>
|
这里整个代码差不多了但是要设置一样更重要的 app/vendor/yiisoft/yii2/captcha/的文件下的CaptchaAction.php这文件中修改getVerifyCode($regenerate = false) 的方法$regenerate参数为true (getVerifyCode($regenerate = true))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.yiichina.com/tutorial/801
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 50
-
2025-05-25 38
-
2025-05-27 45
-
2025-06-04 78
-
2025-06-04 84
热门评论

