NewUser
(Ajay 0162)
February 7, 2012, 10:51am
1
Hello All,
In my application I want that when a user will login he/she can see his/her last login time just like when we do login in user module.So for doing like that that I just followed this link
. So I made my UserIdentity code like this
<?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
{
private $_id;
public function authenticate()
{
$user = User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('lastLoginTime', $user->lastLoginTime);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
$id=Yii::app()->user->id;
$lastLoginTime=Yii::app()->user->lastLoginTime;
}
}
And to show the last login time and user name I changed my layout >> column2.php file like this
<?php $this->beginContent('//layouts/main'); ?>
<div class="container">
<div class="span-19">
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
</div>
<div class="span-5 last">
<div id="sidebar">
<?php if(Yii::app()->user->id):?>
<span class="admin-message">Hello, <span><?php echo yii::app()->user->name;?> </span></span>
<?php echo Yii::app()->user->lastLoginTime;?>
<?php endif;?>
<?php
$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget();
?>
<?php
if(Yii::app()->getModule('user')->isAdmin()):
$this->beginWidget('zii.widgets.CPortlet',array(
'title' => 'Admin Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>array(
array("label"=> "Create User", "url"=>array('/user/admin/create')),
array("label"=> "List User", "url"=> array('/user')),
array("label"=>"Manage Profile","url"=>array('/user/profile')),
array("label"=>"Manage Profile Fields","url"=>array('/user/profileField/admin')),
),
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget(); ?>
<? endif;
?>
</div><!-- sidebar -->
</div>
</div>
<?php $this->endContent(); ?>
It is showing the username after login but when I want to check the last login time it is showing error like:
Property "CWebUser.lastLoginTime" is not defined. Can some one guide me how to do this?
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 11:50am
2
Do you have the lastLoginTime filed in the user table?
On the line where you save this value
$this->setState('lastLoginTime', $user->lastLoginTime);
what is the value of $user->lastLoginTime ?
NewUser
(Ajay 0162)
February 7, 2012, 12:21pm
3
mdomba:
Do you have the lastLoginTime filed in the user table?
On the line where you save this value
$this->setState('lastLoginTime', $user->lastLoginTime);
what is the value of $user->lastLoginTime ?
Yes,lastvisit is already filled.I have used user table for user module where the table is available for lastvisit.I want to get the lastlogin time just like the user module gets the last login time.
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 12:26pm
4
You did not anser the question - what is the value of $user->lastLoginTime on that line ?
NewUser
(Ajay 0162)
February 7, 2012, 12:32pm
5
It has been instructed in this link My link. So I made it.Am I doing wrong there?
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 12:37pm
6
Seems to me you don’t understand how this is working…
The guide you are referring to say:
On the line that I commented the value of the lastLoginTime field is set with setState()… so that field should have a value - the last logged in time.
Note that his guide explains how to get the last login time… it does not explain on how to save it…
NewUser
(Ajay 0162)
February 7, 2012, 12:41pm
7
mdomba:
Seems to me you don’t understand how this is working…
The guide you are referring to say:
On the line that I commented the value of the lastLoginTime field is set with setState()… so that field should have a value - the last logged in time.
Note that his guide explains how to get the last login time… it does not explain on how to save it…
So is there any way to get the lastlogin time?If yes then how?
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 12:46pm
8
Did you read what I wrote before…
You did get the value of the last login time field… problem is that in your database table that field is empty or null…
After the user is logged in… you need to save the current time so that the next time… that time is read as the last login time…
NewUser
(Ajay 0162)
February 7, 2012, 12:51pm
9
mdomba:
Did you read what I wrote before…
You did get the value of the last login time field… problem is that in your database table that field is empty or null…
After the user is logged in… you need to save the current time so that the next time… that time is read as the last login time…
Ok…If I am not wrong than I have to make a field for login time in the database I think, and after that when a user will do login, the login time will be save in that database.and when the user will do another login he will get that time.Is it right?
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 1:23pm
10
Now read again my comment #2 where I asked you
And you answered to this with "yes,…"
deib97
(Deib97)
July 20, 2013, 5:17am
11
Thanks Maurizio Domba
Hmm… Property "CWebUser.lastLoginTime" is not defined.
How to fix this?
nishathul
(Nishathul)
July 20, 2013, 6:20am
12
please do as below
[color=#000000] $this[/color][color=#666600]->[/color][color=#000000]setState[/color][color=#666600]([/color][color=#008800]‘lastLoginTime’[/color][color=#666600],[/color][color=#000000] $user[/color][color=#666600]->[/color][color=#000000]lastLoginTime[/color][color=#666600]);[/color]
please ensure you user model have attribute ‘lastLoginTime’.
Then logout and login to setting the state .
deib97
(Deib97)
July 23, 2013, 4:03am
13
But i used yii-user, and add this code
<?php
private $_id;
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('lastvisit_at', $user->lastvisit_at);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
$id=Yii::app()->user->id;
$lastvisit_at=Yii::app()->user->lastvisit_at;
}
}
in UserIdentity, and my view
<?php echo Yii::app()->user->lastvisit_at;?>
any clue?
nishathul
(Nishathul)
July 23, 2013, 5:13am
14
[size=2]set state and view codes are seems right, please paste here if any error occured[/size]
$this->setState('lastvisit_at', $user->lastvisit_at);
<?php echo Yii::app()->user->lastvisit_at;?>
Add this function to your user model…
public function getlastLoginTime()
{
return date('Y-m-d H:i:s');
}
deib97
(Deib97)
July 31, 2013, 3:42am
16
[size=2]set state and view codes are seems right, please paste here if any error occured[/size]
$this->setState('lastvisit_at', $user->lastvisit_at);
<?php echo Yii::app()->user->lastvisit_at;?>
I have error like this,
Property "RWebUser.lastvisit_at" is not defined.
thanks