hernav
(Victor Mhernandez72)
August 28, 2013, 4:10pm
1
Hi All,
How to display a flash message of last log in time in admin view?
it was set in UserIdentity.php as:
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->check($this->password))
{
$this->_id=$user->id;
$this->username=$user->username;
$this->setState('lastLogin', date("l jS F Y \@\ g:i A", strtotime($user->last_login_time)));
$user->saveAttributes(array(
'last_login_time'=>date("Y-m-d H:i:s", time()),
));
$this->errorCode=self::ERROR_NONE;
}
else
$this->errorCode=self::ERROR_PASSWORD_INVALID;
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
then displayed a message in admin view:
<?php if(!Yii::app()->user->isGuest):?>
<p>
You last logged in on <?php echo Yii::app()->user->lastLogin; ?>.
</p>
<?php endif;?>
the result is:
You last logged in on Wednesday 28th August 2013 @ 5:53 PM.
But the messages stays on the page, I just want to display this message 5 or 10secs after a successfull login.
faridplus
(Faridplusplus)
August 28, 2013, 7:07pm
2
Hi, do you mean that the message should fade out after 5 (or 10) secs? In that case you have to use jQuery:
<?php if(!Yii::app()->user->isGuest):?>
<p id="loginFlash">
You last logged in on <?php echo Yii::app()->user->lastLogin; ?>.
</p>
<?php endif;
Yii::app()->getClientScript()->registerScript('hideLoginFlash', 'jQuery(document).ready(function(){
setTimeOut(function(){
$("#loginFlash").fadeOut();
}, 5000);
});', CClientScript::POS_END);
?>
hernav
(Victor Mhernandez72)
August 29, 2013, 8:20pm
4
faridplus:
Hi, do you mean that the message should fade out after 5 (or 10) secs? In that case you have to use jQuery:
<?php if(!Yii::app()->user->isGuest):?>
<p id="loginFlash">
You last logged in on <?php echo Yii::app()->user->lastLogin; ?>.
</p>
<?php endif;
Yii::app()->getClientScript()->registerScript('hideLoginFlash', 'jQuery(document).ready(function(){
setTimeOut(function(){
$("#loginFlash").fadeOut();
}, 5000);
});', CClientScript::POS_END);
?>
Thanks faridplus for the idea, it’s working now with this code:
<?php if(!Yii::app()->user->isGuest):?>
<div class="flash-success">
You last logged in on <?php echo Yii::app()->user->lastLogin; ?>.
</div>
<?php
Yii::app()->clientScript->registerScript(
'fadeAndHideEffect',
'$(".flash-success").animate({opacity: 1.0}, 5000).fadeOut("slow");'
);
endif; ?>
hernav
(Victor Mhernandez72)
August 30, 2013, 3:45pm
5
Thanks faridplus for the idea, it’s working now with this code:
<?php if(!Yii::app()->user->isGuest):?>
<div class="flash-success">
You last logged in on <?php echo Yii::app()->user->lastLogin; ?>.
</div>
<?php
Yii::app()->clientScript->registerScript(
'fadeAndHideEffect',
'$(".flash-success").animate({opacity: 1.0}, 5000).fadeOut("slow");'
);
endif; ?>
Any idea how to add the logged in username to the flash message?
I want the message like:
Welcome Back (logged in username).You last logged-in on Friday 30th August 2013 @ 7:45 PM.
faridplus
(Faridplusplus)
August 30, 2013, 5:24pm
6
if you’re using the Yii’s default auth classes then simply use:
Yii::app()->user->getName();
hernav
(Victor Mhernandez72)
September 3, 2013, 5:49pm
8
However, the message keeps on displaying every time I visit admin view.
What condition should I put for the flash message to display only after successful log in? instead of this:
<?php if(!Yii::app()->user->isGuest):?>
hernav
(Victor Mhernandez72)
September 6, 2013, 10:48am
9
Thanks Maggie,
on the controller:
Yii::app()->user->setFlash('loginSuccess','Welcome Back <b><?php echo Yii::app()->user->getName(); ?>.</b> You Last Logged-in on <b><?php echo Yii::app()->user->lastLogin; ?>.</b>');
and at view/admin:
<?php if(Yii::app()->user->hasFlash('loginSuccess')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('loginSuccess'); ?>
</div>
<?php
Yii::app()->clientScript->registerScript(
'fadeAndHideEffect',
'$(".flash-success").animate({opacity: 1.0}, 5000).fadeOut("slow");'
);
endif; ?>
the result is:
Welcome Back user->getName(); ?>. You Last Logged-in on user->lastLogin; ?>.
what’s missing to display it like this?
Welcome Back markkeith. You Last Logged-in on Friday 6th September 2013 @ 5:16 AM.
Ankit_Modi
(Ankit Modi)
September 6, 2013, 11:00am
10
check this both the PHP echo statement it’s give me the proper reult or not
hernav
(Victor Mhernandez72)
September 6, 2013, 4:29pm
11
Sorry if this is a dumb question, what to check on PHP echo statement? Do I need to remove or add something?
I really appreciate if you can just give me an example using the code on my post.
Tsunami
(Erik)
September 7, 2013, 7:12pm
12
Yii::app()->user->setFlash('loginSuccess','Welcome Back <b>' . Yii::app()->user->getName() . '.</b> You Last Logged-in on <b>' . Yii::app()->user->lastLogin . '.</b>');