trockey
(Trockey89)
June 17, 2011, 8:58am
1
Hi all…
I have a small problem… I want to save user login time and some other user info… I tried to do that in afterLogin method, which I have override in my WebUser class that extended CWebUser. That work fine when user is not logged in from cookie… Otherwise it throws
Undefined property CWebApplication::$user
So, I wonder why Yii::app()->user is not set in afterLogin method?
Do you have any idea how to solve this problem?
yiippy
(Smartgdi)
June 17, 2011, 10:00am
2
Hi, tried to override afterLogin but it doesn’t work. this ini my code:
components/EWebUser.php
class EWebUser extends CWebUser
{
protected function afterLogin($fromCookie) {
parent::afterLogin($fromCookie);
file_put_contents(Yii::getPathOfAlias('webroot').'/after_login.txt',
'hello world');
}
}
config/main.php
'components'=>array(
'user' => array(
'class' => 'EWebUser',
'allowAutoLogin'=>true,
),
)
is this correct?
elbek
(Elbek Kamoliddinov)
June 17, 2011, 10:11am
3
I assume that the
afterLogin
method should be without param
like that:
afterLogin()
;
This is what i have and it works:
public function afterLogin($fromCookie)
{
if($fromCookie===false)
{
Yii::import('application.modules.user.models.*');
UserDetail::model()->updateByPk($this->id,
array(
'last_login'=>date('Y-m-d H:i:s'),
'last_ip' =>CommonHelper::ipToInt(),
)
);
}
}
Ok we solve one problem when login is not based on cookie data.
if (!$fromCookie)
{
...
// get information stored in session (CWebUser)
...
}
but what about cookie data
else
{
$cookie = Yii::app()->request->cookies['IDENTITY_COOKIE']->value;
}
My question is:
How to retrieve cookie name associated with user identity which I named IDENTITY_COOKIE in pseudo code above?
Here is a part of print_r(Yii::app()->request->cookies);
[CSRF_TOKEN] => CHttpCookie Object
(
[name] => CSRF_TOKEN
........
[PHPSESSID] => CHttpCookie Object
(
[name] => PHPSESSID
........
[2cc85f581b06d3fb213c16d5b35bbdf2] => CHttpCookie Object
(
[name] => 2cc85f581b06d3fb213c16d5b35bbdf2
How to get this identity cookie name: [2cc85f581b06d3fb213c16d5b35bbdf2]
Solution like CWebUser.identityCookie obviously doesn’t work because Yii::app()->user is not defined.
Thanks in advance
ManInTheBox
trockey
(Trockey89)
June 21, 2011, 3:20pm
7
Solved.
You shouldn’t try to get something from session with Yii::app()->user->something, but with $this->something, because $this is the same as Yii::app()->user, but isn’t yet stored in session.