Standard Yii logout timeout

Changed my example again, should work now (added $expire===null condition). viter, maybe you want to try it again?

Can you please show the whole function because I don’t understand where it should be added?

I’ve already changed the example above:

http://www.yiiframework.com/forum/index.php?/topic/13733-standard-yii-logout-timeout/page__view__findpost__p__67836

Cogratulation Mike. It works.

Now we have choice in deciding how to change user’s timeout - using session layer or authentication layer.

Those who might use this feature have to create their class that extends CWebUser (I have class WebUser) and store it in protected/components.

Then add Mike’s function (don’t forget ‘public $authExpires;’) to your class.

And finally add this to your conf/main.php (components section)


'user'=>array(

	// enable cookie-based authentication

	'allowAutoLogin'=>true,

        'class' => 'YourClassName',

        'authExpires' => desired_timeout_in_seconds,

),

That’s all.

Mike, thank you.

By the way Mike. Why don’t you write a more detailed howto in wiki?

That’s my solution to define a timeout for session with a redirect/refresh after expired session.

protected/config/main.php : (define the session timeout)


$sessionTimeout = 5; // 5 secondes


return array(

	'params'=>require(dirname(__FILE__).'/params.php'),

	'components'=>array(

		'session' => array(

			'class' => 'CDbHttpSession',

			'timeout' => $sessionTimeout,

		),

	),

);

protected/config/params.php :


// this contains the application parameters that can be maintained via GUI

return array(

	'session_timeout'=> $sessionTimeout,

);

protected/views/layout/main.php : (define the refresh)

[html]<html>

<head>

&lt;?php if (&#33;Yii::app()-&gt;user-&gt;isGuest) {?&gt;


	&lt;meta http-equiv=&quot;refresh&quot; content=&quot;&lt;?php echo Yii::app()-&gt;params['session_timeout'];?&gt;;&quot;/&gt;


&lt;?php }?&gt;

</head>

<body>

</body>

</html>[/html]

hello … I follow the example of your code and runs perfectly …

I want to add any he has timed out, appeared alert or setFlash or redirect

how to do it?

i try





  $this->logout();

            $isGuest=true;

  echo "<script>alert('you need to login to perform this action!');</script>";



but does not appear, or I was wrong to put it?

hi Advanced Member,

Your solution is working if session time out after click a mene , new page request,

But when i click in cgridview sorting, pagination, search its not redirect to login page.

Thanks,

Thanks …It helped me…

but m facing a problem …i hv captcha code at my login page…and on entering login credentials with correct captcha it says captcha is incorrect…and in second try it gets login…

Old topic, but just ran into this.

Appearantly normal PHP session handling works as follows when the server receives a request:

  • Check if received session is valid?

    • If it is, set expire time again.
  • Are we going to do garbage collect (default 1% chance)

    • If we are, remove all expired session

This means, when you login from one machine and no-one else is doing requests, your session will remain active while your browser remains open. You can only force a timeout on application level as previous posters have shown.

Isn’t it strange that PHP doesn’t handle this automatically? Or is it just because single application user is an unimportant/rare case or because of possible performance impact?

Yes there is. I too got it after many attempts. It is working very fine. In the main.php in config folder just find out the user array. And set the "authTimeout" property to number of seconds that you want.

Below is the code:

‘user’ => array(

        .............


        .............


        'allowAutoLogin' =&gt; true,


        'authTimeout' =&gt; 5,


        .............


        .............


    ),  

It will sure logout the user after 5 seconds. :)

Yes it works for me. What changes do i need to do to make it work with single browser?

Thanks,

Nivas

Try this,




	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

			'class'=>'CWebUser',

			'autoUpdateFlash' => false, // add this line to disable the flash counter

			'loginUrl'=> "/site/login",

			'authTimeout' => 31104000, // A year

			'absoluteAuthTimeout' => 31104000,

			'autoRenewCookie'=>true

		),

)