本文实例讲述了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程序设计有所帮助。
本文向大家介绍Django框架登录加上验证码校验实现验证功能示例,包括了Django框架登录加上验证码校验实现验证功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Django框架登录加上验证码校验实现验证功能。分享给大家供大家参考,具体如下: 验证码生成函数 login.html html文件加上<img src="/verify_code" alt="">…当然这里还需要配置ur
本文向大家介绍springmvc下实现登录验证码功能示例,包括了springmvc下实现登录验证码功能示例的使用技巧和注意事项,需要的朋友参考一下 总体思路,简单讲,就是后台生成图片同时将图片信息保存在session,前端显示图片,输入验证码信息后提交表单到后台,取出存放在session里的验证码信息,与表单提交的验证码信息核对。 点击验证码图片时,通过jquery重新请求后台生成验证码图片方法,
本文向大家介绍Yii框架弹出框功能示例,包括了Yii框架弹出框功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Yii框架弹出框功能。分享给大家供大家参考,具体如下: 更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总
本文向大家介绍Spring Security实现验证码登录功能,包括了Spring Security实现验证码登录功能的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Spring Security实现验证码登录功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在spring security实现登录注销功能的基础上进行开发。 1、添
本文向大家介绍thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例,包括了thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了thinkphp 5框架实现登陆,登出及session登陆状态检测功能。分享给大家供大家参考,具体如下: 1,访问http://localhost/tp5/admin.php
本文向大家介绍Yii框架表单提交验证功能分析,包括了Yii框架表单提交验证功能分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Yii框架表单提交验证功能。分享给大家供大家参考,具体如下: 一、前端提交的三种方式 前面已经提出,表单提交一共只有三种方式。 1. 前端原生html (1)原生html标签 首先,直接使用html标签的input,a等; 其次,使用css进行布局。 再次,使用