yii 从数据库获取用户名、密码

原创
2013/09/18 18:23
阅读数 121

修改 protected/components/UserIdentity.php

class UserIdentity extends CUserIdentity {

private $_id; public function authenticate() { $username=strtolower($this->username);//将用户输入的用户名变为小写(防止因大小写重名) $user=User::model()->find('LOWER(username)=?',array($username)) //'LOWER(username)此处的username 为数据库user表里的字段 if($user==null){ $this->errorCode=self::ERROR_USERNAME_INVALID; }else{ if(!$user->validatePassword($this->password)){ //validatePassword 为模型user类里的方法 $this->errorCode=self::ERROR_PASSWORD_INVALID; }else{ $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } } return $this->errorCode===self::ERROR_NONE; }

public function getId(){ return $this->_id; } }

在模型user类中(models/user.php)添加如下

public function validatePassword($password){ return $this->encypt($password)===$this->password; }

public function encypt($pass){ return md5($pass); }

展开阅读全文

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部