Problem To Get Loged In User Id After Login


I’m very very beginner in Yii world. Somebody please tell how do I get a user id of logged in user I mean the primary key of logged in user.

I just use it in my UserIdentity.php class

private $id;

public function getId()


return $this->id;


I called it from layout/main.php file this way

echo Yii::app()->user->getId();

but it returned me controller id but I need logged in user table id.

Please help me…

CUserIdentity already provides that functionality, so you shouldn’t need to override it. Yii::app()->user->id should provide the value you need.

Thank you for your help.

But I just removed my function getID() from UserIdentity.php and then I called echo Yii::app()->user->getId(); form layout/main.php but it returned me controller name not logged in user table id.

:( :(

Yii::app()->user->getId() gives you this:

So inspect your code, seems like you have overriden something.

Places to look:

UserIdentity (getId(), authenticate())

WebUser (if any)



Thank you I just fixed it but i just faced another problem this function print "Yii::app()->user->getId()" my user name but I need user row id.

You can override it in UserIdentity::authenticate()

$this->setState('id', $user->id);

Thank you. Thanks a lot for you kindness. God bless you…

:) :) :)

No it doesn’t! The default implementation of of getId() is to return the username, same as getUsername().

To answer the OP’s question. You might need to change your variable name, $this->id maybe used by Yii for the name of the current object.

private $_id;

public function getId() {

   return $this->_id;