Yii Framework Forum: get user id after login - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

get user id after login how do i get a user id after login Rate Topic: -----

#1 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 18 March 2011 - 06:09 AM

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 process.so any body give me some guidance that how could i complete this work.

thanks in advance..!!1
CodeSutra
0

#2 User is offline   caramelon 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 19-January 11
  • Location:Neandertal

Posted 18 March 2011 - 04:29 PM

jayant,
are you sure you have

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


and not

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


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

#3 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 09-April 10

Posted 19 March 2011 - 12:55 AM

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

#4 User is offline   Command 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 06-September 10
  • Location:Berlin

Posted 19 March 2011 - 07:25 PM

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 ;) )
0

#5 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 22 March 2011 - 01:33 AM

i've checked my userIdentity class and solve my problem..
thnx for your replies..!!! :rolleyes:
CodeSutra
0

#6 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 22 March 2011 - 01:40 AM

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..!!!
CodeSutra
0

#7 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 09-April 10

Posted 22 March 2011 - 02:08 AM

After successful login, redirect to "user/id/$id" by
$this->redirect(array('/user/','id'=>Yii::app()->user->getId()));
.

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?

View Postjayant, on 22 March 2011 - 01:40 AM, said:

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..!!!

1

#8 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 22 March 2011 - 04:09 AM

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 folder.one 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..!! :)
CodeSutra
0

#9 User is offline   nazum 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 21-March 11

Posted 22 March 2011 - 04:17 AM

View Postjayant, on 22 March 2011 - 04:09 AM, said:

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

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


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.
0

#10 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 22 March 2011 - 06:10 AM

View Postnazum, on 22 March 2011 - 04:17 AM, said:

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.!!
CodeSutra
0

#11 User is offline   morcen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 268
  • Joined: 25-October 10
  • Location:Marikina City, Philippines

Posted 22 March 2011 - 08:04 AM

View Postjayant, on 22 March 2011 - 06:10 AM, said:

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.
Tell your tail.
Don't be shy to upvote a post if it was able to help you
0

#12 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 09-April 10

Posted 22 March 2011 - 09:06 AM

Hi,

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.

Index.php
    <?php $this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
)); ?>


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.

View Postjayant, on 22 March 2011 - 04:09 AM, said:

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 folder.one 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..!! :)

0

#13 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 27 March 2011 - 11:32 PM

View Postmacinville, on 22 March 2011 - 08:04 AM, said:

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.


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.
CodeSutra
0

#14 User is offline   liu1084 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 16-June 09

Posted 28 March 2011 - 12:09 AM

<?php

/**
 * 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);
        }

        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}

Usge in your application :
Yii::app()->user->getId() or Yii::app()->user->id

study yii framework!
0

#15 User is offline   heera 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 18-April 11

Posted 03 May 2011 - 03:05 AM

Yii::app()->user->id
0

#16 User is offline   jsmith 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 23-March 09

Posted 11 June 2011 - 01:46 PM

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?
0

#17 User is offline   vi_na_ta_ba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 29-June 11
  • Location:vietnam

Posted 29 June 2011 - 05:25 AM

To get username i use :
Yii::app()->user->id 

To get an id (the integer unique ) ,try to use :
User::model()->find('username=:username',array(':username'=>$Yii::app()->user->id))->id

1

#18 User is offline   Nooruddin Lakhani 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-March 12

Posted 03 April 2012 - 05:18 AM

View Postjayant, on 22 March 2011 - 01:33 AM, said:

i've checked my userIdentity class and solve my problem..
thnx for your replies..!!! :rolleyes:

how you got the login user id? what's the solution?
0

#19 User is offline   karasawaz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-August 12

Posted 31 October 2012 - 05:43 AM

in useridentity.php, i found

Quote

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

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

Quote

$id=Yii::app()->User->id;


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

Quote

$record=ItStaff::model()->findByAttributes(array('username'=>$this->username));

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...
0

#20 User is offline   varadk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 25-July 14

Posted 06 September 2014 - 02:22 AM

View Postcodesutra, on 18 March 2011 - 06:09 AM, said:

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 process.so any body give me some guidance that how could i complete this work.

thanks in advance..!!1



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 $this->getState('id');
}

where ever userid is required use
Yii::app()->user->userId();
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users