YII 框架 第七天 登陆
2013-09-19 15:08
309 查看
public function actionLogin() { $user_login = new LoginForm(); //LoginForm 是 YII自带的一个文件 需要配置 if(isset($_POST['LoginForm'])) { $user_login->attributes=$_POST['LoginForm']; if($user_login->validate() && $user_login->login()) //validate 验证 login 设置session { $this->redirect('index.php'); } } $this->render('login',array('user_login'=>$user_login)); }
LoginForm 文件
public function authenticate($attribute,$params) { if(!$this->hasErrors()) { $this->_identity=new UserIdentity($this->username,$this->password); //需要到 UserIdentity中配置 if(!$this->_identity->authenticate()) $this->addError('password','用户名或密码错误'); } }
在 components 中 UserIdentity
public function authenticate() { $user_model = User::model()->find('username=:name',array(':name'=>$this->username)); if($user_model === NULL) $this->errorCode=self::ERROR_USERNAME_INVALID; elseif($user_model->password !== $this->password) $this->errorCode=self::ERROR_PASSWORD_INVALID; else $this->errorCode=self::ERROR_NONE; return !$this->errorCode; }
下面 就 是简单的了
public function rules() { return array( // username and password are required array('username', 'required' ,'message'=>'用户名必填'), array('password', 'required' ,'message'=>'密码必填'), // rememberMe needs to be a boolean array('rememberMe', 'boolean'), // password needs to be authenticated array('password', 'authenticate'), ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'rememberMe'=>'记住我', 'username'=>'用户名', 'password'=>'密码', ); } /** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate($attribute,$params) { if(!$this->hasErrors()) { $this->_identity=new UserIdentity($this->username,$this->password); //需要到 UserIdentity中配置 if(!$this->_identity->authenticate()) $this->addError('password','用户名或密码错误'); } }
相关文章推荐
- yii框架非法登陆
- 详细解读PHP的Yii框架中登陆功能的实现
- YII 框架 第七天(2) 验证码
- 关于后盾网yii框架的学习小结(6)--数据库连接配置与模型定义与后台登陆验
- YII 框架 第七天(3) 前后台session区分
- 关于后盾网yii框架的学习小结(6)--数据库连接配置与模型定义与后台登陆验
- 详细解读PHP的Yii框架中登陆功能的实现
- YII 框架 第七天(4) session cookie
- YII 框架 第七天(5) 路径别名
- Yii框架使用总结
- yii框架中findall方法取数据使用总结,包括select各种条件,where条件,order by条件,limit限制以及使用单纯sql语句query时占位符的使用等
- Yii2.0高级框架数据库增删改查的一些操作(转)
- yii框架中省市三级联动
- php yii框架web项目连接数据库以及crud操作
- YII 框架 第九天(2)片段缓存的缓存依赖(实用)
- YII 框架 第十天(4)面包屑导航
- yii框架源码分析之Yii::createWebApplication()->run() 执行过程分析
- yii框架
- Yii框架发送邮件
- Yii框架入门教程(博客教程、权威指南、类手册)