Set Extra Value in SESSION, Like(User Id not Name)

It is very easy to set extra value in yii framework $_SESSION. Just follow Line no-15 and 24, 25. The setState() function helps you.

  1. class UserIdentity extends CUserIdentity
  2. {
  3.     /**
  4.      * Authenticates a user.
  5.      * The example implementation makes sure if the username and password
  6.      * are both ’demo’.
  7.      * In practical applications, this should be changed to authenticate
  8.      * against some persistent user identity storage (e.g. database).
  9.      * @return boolean whether authentication succeeds.
  10.      */
  11.     private $_id;
  12.     
  13.     public function authenticate()
  14.     {        
  15.         $user=UserInfo::model()->findByAttributes(array(‘user_name’=>$this->username));
  16.         
  17.         if($user===null)
  18.             $this->errorCode=self::ERROR_USERNAME_INVALID;
  19.         else if($user->password!==$this->password)
  20.             $this->errorCode=self::ERROR_PASSWORD_INVALID;
  21.         else
  22.         {
  23.             $this->_id = $user->id;
  24.             $this->setState(‘userId’, $user->id);
  25.             $this->setState(‘typeId’, $user->type);
  26.             $this->errorCode=self::ERROR_NONE;
  27.         }
  28.         return !$this->errorCode;
  29.  
  30.     }
  31.     
  32.     public function getId()
  33.     {
  34.         return $this->_id;
  35.     }
  36. }

Rajib Kumar Rakhmit

Yii Framework

Rajib Kumar Rakhmit

Rajib Kumar Rakhmit

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>