Yii框架实现的验证码、登录及退出功能示例
程序员文章站
2024-03-09 10:51:41
本文实例讲述了yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:
捣鼓了一下午,总算走通了,下面贴出代码。
model
...
本文实例讲述了yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:
捣鼓了一下午,总算走通了,下面贴出代码。
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_account; public $a_password; public $rememberme; public $verifycode; public $_identity; public function rules() { return array( array('verifycode', 'captcha', 'allowempty' => !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' => '验证码' ); } }
注:indexform也可以写成loginform,只是系统内已经有了,我就没有替换它,同时注意看自己用户表的字段,一般是password和username,而我的是a_account和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_account'); ?> <?php echo $form->textfield($model,'a_account'); ?> <?php echo $form->error($model,'a_account'); ?> </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/components下的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 succeeds. */ 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_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优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于yii框架的php程序设计有所帮助。