get user id after login

somebody tell how do i get a userid of logged in user.

i’ve already try to a code to get a userid in UserIdentity class…but when i try to get user ids with Yii:app()->user->getId()

it didn’t return me appropriate result.

and one more thing i am also trying to redirect user on different page after login rather than index page of site.can some body tell me to how do i change return url after login.

because i want to redirect user to his/her profile after login any body give me some guidance that how could i complete this work.

thanks in advance…!!1


are you sure you have


and not


Just checking the simplest thing. Also, what error do you get?

Can you post your UserIdentity class? This class should be setting the Id in the authenticate method.

im just using Yii::app()->user->id but i think its the same as the getId().

If this doesnt work, please post your Useridenty class (like waterloomatt suggested ;) )

i’ve checked my userIdentity class and solve my problem…

thnx for your replies…!!! :rolleyes:

hey…but anybody of you guys cal explain me how could i create a user profile of registered usr…coz i ma not using yii-user module for my project…and trying to write my own code…!!!

i want to redirect a user to his profile page after login…so would you please suggest me something about it…

i have an action with name actionProfile…but i am not getting how could i pass a user id in this function so i can get info about this particular user and show this info on view file…

i have gone through user module’s user controller and try to check some piece of code for action profile…but i was stuck when i saw a code of loadmodule action… :-[

colud you please guys help me out here…!!!

After successful login, redirect to "user/id/$id" by



In that User controller action View, retrieve the user from the DB and pass it to the view where you can access all the users information.

Does this help?

yes i’ve done same thing…and it does work for me…

but i have one question in my made…if you don’t mind then please clear it out for me…

when we call a view action …then there is a two files were created in views is view.php and otherone is _view.php

could you please tell me what is a need of these two different files.

we can not use a method like cakephp as we can set a variable or array from action and can retrieve a data from it in view file.

dose it make any sense. then please let me know a exact piece of code to perform it.

coz i was working with cakephp since from last few year and trying to establish my hands with yii also…!!

thanks in advance…!! :)

The view.php file is used to see one record’s details.

The _view.php is used by the index.php file to display how each of the items listed looks. It creates a format for each row returned.

hmmmm…it means whenever i have to display a single record of db then we have to create two files same as view action…

Does it make any sense.!!

Hi jayant,

No there’s no need to create two files whenever there’s a need to display a single record (I actually don’t know where did that came from, there must be a misunderstanding somewhere).

If you want to write your own code, it is essential that you have the basic understanding of how Yii works. But if I may suggest, since you don’t want to make use of the Yii-user module for your application but you want to write a module just like that, then just download the Yii-user module then study how it works. Not everyone here will always be available to answer your questions, but the code will always be there for you to experiment on. Besides, example is the best teacher so as they say.


To pass variables to the view set the 2nd parameter in the render method. See below. In your views, you can then use the key of the array as a variable. In this case, $model.


     * Returns the data model based on the primary key given in the GET variable.

     * If the data model is not found, an HTTP exception will be raised.

     * @param integer the ID of the model to be loaded


    public function loadModel($id)


        $model = User::model()->findByPk((int) $id);

        if ($model === null)

            throw new CHttpException(404, 'The requested page does not exist.');

        return $model;



     * Displays a particular model.

     * @param integer $id the ID of the model to be displayed


    public function actionView($id)


        $this->render('view', array(

            'model' => $this->loadModel($id),



The view.php file is used to display details about the record.

If you look at the default files after CRUDing your table, you’ll see index.php and _view.php. These two normally work in conjunction with eachother. Ex - index.php calls _view.php. The _view.php is simply a template file and the index.php passes the dataProvider variable to that view to display.


    <?php $this->widget('zii.widgets.CListView', array(



)); ?>

To answer you first question, the actionView() method should use the view.php file and the actionIndex() method should use the index.php which will call the _view.php.

Thanks a lot macinville.

yeah i’ve done my work witrh the same approach as you told me.and yes i did.i’ve study a yii user module and write my own code for my application.

Currently i am working on this app.and when it will be finished.i’ll explore it to you guys…!!

thanks again macinville.



 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.


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.


    private $_id = NULL;

    public function authenticate() {

        $criteria = new CDbCriteria;

        $criteria->condition = 'username = :username';

        $criteria->params = array(':username' => $this->username);

        $user = User::model()->find($criteria);

        if (!isset($this->username) || NUll === $user || !isset($this->password)) {

            $this->errorCode = self::ERROR_USERNAME_INVALID;

        } else if ($user->encryptPassword($this->password) !== $user->password) {

            $this->errorCode = self::ERROR_PASSWORD_INVALID;

        } else {

            $this->errorCode = self::ERROR_NONE;

            $this->_id = $user->id;

            if (NULL === $user->last_login_time) {

                $lastLoginTime = time();

            } else {

                $lastLoginTime = strtotime($user->last_login_time);


            $this->setState('lastLoginTime', $lastLoginTime);




    public function getId() {

        return $this->_id;



Usge in your application :

Yii::app()->user->getId() or Yii::app()->user->id


Its not possible to use Yii::app()->user in afterLogin() method when allowAutoLogin is true. I mean for user who doesnt use "remember me" its ok, but it fails when afterLogin() is called inside restoreFromCookie method.

Im getting:

PHP Error

Undefined property: CWebApplication::$user

Anybody can help?

To get username i use :


To get an id (the integer unique ) ,try to use :


how you got the login user id? what’s the solution?

in useridentity.php, i found

to get the logged-in-user id

when i want to show the logged-in-user id, i just used

and then do the echo $id

what should i do when i want to get another variable for example fullname variable from the database

my record in useridentity is this

it just return the login username and id… not the other…

*Nb: Donnee…

Just need to update the CWebUser.php and add the function getFullname setFullname etc…

define WebUser class which extends CWebUser .I useridentity set userid using setState(); method

as $this->setState(‘id’,$user->userid);

and access it in WebUser as

public function userId()


	return &#036;this-&gt;getState('id');


where ever userid is required use