本文实例讲述了Yii使用Captcha验证码的方法。分享给大家供大家参考,具体如下:
详细代码可参考:yii自带的示例代码post项目,里面有一个contact表单用到了验证码.
1. Model:
将验证码加入UserLogin的一个属性:
?
|
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
|
class UserLogin extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $verifyCode;
public function rules()
{
return array(
// username and password are required
array('username, password,verifyCode', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>Yii::t('user',"Remember me next time"),
'username'=>Yii::t('user',"username or email"),
'password'=>Yii::t('user',"password"),
'verifyCode'=>Yii::t('user','Verification Code'),
);
}
}
|
2. Controller
在LoginController控制器加入映射动作CCaptchaAction
?
|
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
|
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xf4f4f4,
'padding'=>0,
'height'=>30,
'maxLength'=>4,
),
);
}
ublic function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
//在此核对验证码
if($this->createAction('captcha')->validate($model->verifyCode, false))
{
// validate user input and redirect to previous page if valid
if($model->validate()) {
//admin login only
if( Yii::app()->getModule('user')->isAdmin()==1 )
{
$this->lastViset();
if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
$this->redirect(Yii::app()->controller->module->returnUrl);
else
$this->redirect(Yii::app()->user->returnUrl);
}else
{//if no admin when login out
$this->redirect(Yii::app()->controller->module->logoutUrl);
}
}
}else
{//提示错误
$model->addError('verifyCode','验证码不对');
}
}
// display the login form
$this->render('/user/login',array('model'=>$model));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
|
在验证用户名密码前,检查验证码:
?
|
1
2
|
if($this->createAction('captcha')->validate($model->verifyCode, false))
{
|
3. view
在视图中显示验证码图片,输入框
?
|
1
2
3
|
<?php $this->widget('CCaptcha'); ?>
<?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>
<img src="/wp-content/uploads/202505/29/18772.jpg" alt="">
|
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 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-05-29 86
-
springboot整合ehcache 实现支付超时限制的方法
2025-05-27 68 -
2025-05-25 85
-
2025-05-29 48
-
2025-05-27 58
热门评论

