Yii框架实现的验证码、登录及退出成果示例
发布时间:2021-01-17 20:24:24 所属栏目:编程 来源:网络整理
导读:本篇章节讲授Yii框架实现的验证码、登录及退出成果。供各人参考研究详细如下: 捣鼓了一下战书,总算走通了,下面贴出代码。 Model 注:我的用户表是auth,以是模子是Auth.php !CCaptcha::checkRequirements(),'message
本篇章节讲授Yii框架实现的验证码、登录及退出成果。分享给各人供各人参考,详细如下: 捣鼓了一下战书,总算走通了,下面贴出代码。 Model 注:我的用户表是auth,以是模子是Auth.php !CCaptcha::checkRequirements(),'message'=>'请输入正确的验证码'),array('a_account','required','message' => '用户名必填'),array('a_password','message' => '暗码必填'),'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' => '验证码' ); } }注:IndexForm也可以写成LoginForm,只是体系内已经有了,我就没有替代它,同时留意看本身用户表的字段,一样平常是password和username,而我的是a_account和a_password Controller 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'); } }注:第一个要领是添加验证码的 view beginWidget('CActiveForm',array( 'id' => 'login-form','enableClientValidation' => true,'clientOptions' => array( 'validateOnSubmit' => true ) )); ?> labelEx($model,'a_account'); ?> textField($model,'a_account'); ?> error($model,'a_account'); ?> labelEx($model,'a_password'); ?> passwordField($model,'a_password'); ?> error($model,'a_password'); ?> labelEx($model,'verifyCode'); ?> widget('CCaptcha'); ?> textField($model,'verifyCode'); ?> error($model,'verifyCode'); ?> checkBox($model,'rememberMe'); ?> label($model,'rememberMe'); ?> error($model,'rememberMe'); ?> endWidget(); ?>同时修改项目下protected/components下的UserIdentity.php password
'demo'=>'demo','admin'=>'admin',);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
*/
$user_model = Auth::model()->find('a_account=:name',array(':name'=>$this->username));
if($user_model === null){
$this -> errorCode = self::ERROR_USERNAME_INVALID;
return false;
} else if ($user_model->a_password !== md5($this -> password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
return false;
} else {
$this->errorCode=self::ERROR_NONE;
return true;
}
}
}
更多关于Yii相干内容感乐趣的读者可查察本站专题:《》、《》、《》、《》、《》、《》及《》 但愿本文所述对各人基于Yii框架的PHP措施计划有所辅佐。 (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |