您的位置:首页 > 编程语言 > PHP开发

YII登录过程简单总结

2013-12-04 20:42 381 查看
用户的登录、过程是一个比较费劲的事,还在Yii提供了组建支持,熟练的使用该组件,在开发中可以大大的减少时间。

在siteController中有如下actionLogin()

if ($isGuest){
$model=new LoginForm;
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];

if($model->validate() && $model->login()){
ob_start();
$this->redirect(array("fortune/aucno"));
}
}

$this->render('login',array('model'=>$model));其中$model->login()调用的是model/LoginForm.php中的login方法,继续追踪改login()方法:
public function login()
{

if($this->_identity===null)
{

$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{

$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}

$this->_identity=new UserIdentity($this->username,$this->password);执行的是链接数据据库验证用户名密码
Yii::app()->user->login($this->_identity,$duration);//这条语句执行的是设置session或者cookie操作,具体可以查看yii参考文档

该方法可重写,代码如下很容易看懂:

$user=new User();
$username=trim($this->username);
$user=$user->find("username='".$username."'");
if(isset($user)){
$password=$user->password;
$userid=$user->id;
$this->id=$userid;
}
if(!isset($this->username) or !isset($user) or $user=""){

$this->errorCode='323';//self::ERROR_USERNAME_INVALID;
}
else if($password!=md5($this->password)){

$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else{

$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: