Yii框架实现的验证码、登录及退出功能示例
网络编程 2021-07-05 08:23www.168986.cn编程入门
这篇文章主要介绍了Yii框架实现的验证码、登录及退出功能,结合具体实例形式分析了基于Yii框架实现的用户验证登录及退出操作相关步骤与操作技巧,需要的朋友可以参考下
本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下
捣鼓了一下午,总算走通了,狼蚁网站SEO优化贴出代码。
Model
<?php class Auth extends CActiveRecord { public static function model($className = __CLASS__) { return parent::model($className); } public function tableName() { return '{{auth}}'; } }
注我的用户表是auth,所以模型是Auth.php
<?php class IndexForm extends CFormModel { public $a_aount; public $a_password; public $rememberMe; public $verifyCode; public $_identity; public function rules() { return array( array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message'=>'请输入正确的验证码'), array('a_aount', '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_aount, $this->a_password); if (!$this->_identity->authenticate()) { $this->addError('a_password', '用户名或密码不存在'); } } } public function login() { if ($this->_identity === null) { $this->_identity = new UserIdentity($this->a_aount, $this->a_password); $this->_identity->authenticate(); } if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { $duration = $this->rememberMe ? 6060247 : 0; Yii::app()->user->login($this->_identity, $duration); return true; } else { return false; } } public function attributeLabels() { return array( 'a_aount' => '用户名', 'a_password' => '密码', 'rememberMe' => '记住登录状态', 'verifyCode' => '验证码' ); } }
注IndexForm也可以写成LoginForm,只是系统内已经有了,我就没有替换它,注意看自己用户表的字段,一般是password和username,而我的是a_aount和a_password
Controller
<?php class IndexController extends Controller { public function actions() { return array( 'captcha' => array( 'class' => 'CCaptchaAction', 'width'=>100, 'height'=>50 ) ); } public function actionLogin() { if (Yii::app()->user->id) { echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>"; } else { $model = new IndexForm(); if (isset($_POST['IndexForm'])) { $model->attributes = $_POST['IndexForm']; if ($model->validate() && $model->login()) { echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";exit; } } $this->render('login', array('model' => $model)); } } public function actionLogout() { Yii::app()->user->logout(); $this->redirect(SITE_URL . 'admin/index/login'); } }
注第一个方法是添加验证码的
view
<meta http-equiv="content-type" content="text/html;charset=utf-8"> <?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'login-form', 'enableClientValidation' => true, 'clientOptions' => array( 'validateOnSubmit' => true ) )); ?> <div class="row"> <?php echo $form->labelEx($model,'a_aount'); ?> <?php echo $form->textField($model,'a_aount'); ?> <?php echo $form->error($model,'a_aount'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'a_password'); ?> <?php echo $form->passwordField($model,'a_password'); ?> <?php echo $form->error($model,'a_password'); ?> </div> <?php if(CCaptcha::checkRequirements()) { ?> <div class="row"> <?php echo $form->labelEx($model, 'verifyCode'); ?> <?php $this->widget('CCaptcha'); ?> <?php echo $form->textField($model, 'verifyCode'); ?> <?php echo $form->error($model, 'verifyCode'); ?> </div> <?php } ?> <div class="row rememberMe"> <?php echo $form->checkBox($model,'rememberMe'); ?> <?php echo $form->label($model,'rememberMe'); ?> <?php echo $form->error($model,'rememberMe'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Submit'); ?> </div> <?php $this->endWidget(); ?>
修改项目下protected/ponents下的UserIdentity.php
<?php / UserIdentity represents the data needed to identity a user. It contains the authentication method that checks if the provided data can identity the user. / class UserIdentity extends CUserIdentity { / Authenticates a user. The example implementation makes sure if the username and password are both 'demo'. In practical applications, this should be changed to authenticate against some persistent user identity storage (e.g. database). @return boolean whether authentication sueeds. / public function authenticate() { / $users=array( // username => 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_aount=: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程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程