Hi i’m only new to php framework. i want to ask, how yii framework save the user after login ? In the php general , i usually use session. How about yii frameworks ?
Hi i’m only new to php framework. i want to ask, how yii framework save the user after login ? In the php general , i usually use session. How about yii frameworks ?
go through the
>login
>components/useridentity
you will get the answer
for a basic app that u created , the UserIdentity looks like this
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;
}
}
if you change it from database
class UserIdentity extends CUserIdentity
{
private $_id;
const ERROR_EMAIL_INVALID=3;
const ERROR_STATUS_NOTACTIV=4;
const ERROR_STATUS_BAN=5;
/**
* 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()
{
if (strpos($this->username,"@")) {
$user=User::model()->notsafe()->findByAttributes(array('email'=>$this->username));
} else {
$user=User::model()->notsafe()->findByAttributes(array('username'=>$this->username));
}
if($user===null)
if (strpos($this->username,"@")) {
$this->errorCode=self::ERROR_EMAIL_INVALID;
} else {
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else if($user->status==0&&Yii::app()->getModule('user')->loginNotActiv==false)
$this->errorCode=self::ERROR_STATUS_NOTACTIV;
else if($user->status==-1)
$this->errorCode=self::ERROR_STATUS_BAN;
else {
$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
/**
* @return integer the ID of the user record
*/
public function getId()
{
return $this->_id;
}
}
you will get user id using the code
Yii::app()->user->id
Cookie or session, depends how your application is set (with or without “remember me” option). You can find more in CWebUser
Sorry Rajith, didn’t see you’ve already replied
thanks a lot rajith you’re so helpfull.
can you help me, how to destroy session or cookie in the login page ?
when the user logs out, you may want to formally eradicate the session. To do so, call
Yii::app()->session->clear()
to remove all of the session variables. Then call
Yii::app()->session->destroy()
to get rid of the actual data stored on the server.
This article explains what i said
wow, thanks again rajith.