Yii框架实现的验证码、登录及退出功能示例
框架(架构)  /  管理员 发布于 6年前   138
本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下: 捣鼓了一下午,总算走通了,下面贴出代码。 Model 注:我的用户表是auth,所以模型是Auth.php 注:IndexForm也可以写成LoginForm,只是系统内已经有了,我就没有替换它,同时注意看自己用户表的字段,一般是password和username,而我的是a_account和a_password Controller 注:第一个方法是添加验证码的 view 123 在 原梓番博客 在 博主 在 1111 在 路人 在 !CCaptcha::checkRequirements(), 'message'=>'请输入正确的验证码'), array('a_account', 'required', 'message' => '用户名必填'), array('a_password', 'required', 'message' => '密码必填'), array('a_password', 'authenticate'), array('rememberMe', 'boolean'), ); } public function authenticate($attribute, $params) { if (!$this->hasErrors()) { $this->_identity = new UserIdentity($this->a_account, $this->a_password); if (!$this->_identity->authenticate()) { $this->addError('a_password', '用户名或密码不存在'); } } } public function login() { if ($this->_identity === null) { $this->_identity = new UserIdentity($this->a_account, $this->a_password); $this->_identity->authenticate(); } if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { $duration = $this->rememberMe ? 60*60*24*7 : 0; Yii::app()->user->login($this->_identity, $duration); return true; } else { return false; } } public function attributeLabels() { return array( 'a_account' => '用户名', 'a_password' => '密码', 'rememberMe' => '记住登录状态', 'verifyCode' => '验证码' ); }}
array( 'class' => 'CCaptchaAction', 'width'=>100, 'height'=>50 ) ); } public function actionLogin() { if (Yii::app()->user->id) { echo "欢迎" . Yii::app()->user->id . ",退出"; } else { $model = new IndexForm(); if (isset($_POST['IndexForm'])) { $model->attributes = $_POST['IndexForm']; if ($model->validate() && $model->login()) { echo "欢迎" . Yii::app()->user->id . ",退出";exit; } } $this->render('login', array('model' => $model)); } } public function actionLogout() { Yii::app()->user->logout(); $this->redirect(SITE_URL . 'admin/index/login'); }}
beginWidget('CActiveForm', array( 'id'=> 'login-form', 'enableClientValidation' => true, 'clientOptions' => array( 'validateOnSubmit' => true )));?>
上一条:
Yii框架使用魔术方法实现跨文件调用功能示例
下一条:
Yii框架实现图片上传的方法详解
Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号