YII登录过程简单总结

本文简单总结了Yii框架中的用户登录过程,重点介绍了在siteController中的actionLogin()方法,特别是Yii::app()->user->login()这行代码,该方法用于设置session或cookie,实现用户身份验证。通过熟悉并利用Yii提供的组件,可以有效提升开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用户的登录、过程是一个比较费劲的事,还在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;		



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值