[Solved] Last Log In Time - Flash Message

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.

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

?>



check this wiki

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.

if you’re using the Yii’s default auth classes then simply use:


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

Thanks faridplus.

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):?>



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.

check this both the PHP echo statement it’s give me the proper reult or not

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.


Yii::app()->user->setFlash('loginSuccess','Welcome Back <b>' . Yii::app()->user->getName() . '.</b> You Last Logged-in on <b>' . Yii::app()->user->lastLogin . '.</b>');

Thanks Tsunami.