Laravel 4.2 - 使用Event时用户会话超时时无法更新我的数据库

Laravel 4.2  - 使用Event时用户会话超时时无法更新我的数据库

问题描述:

I currently have a problem with my laravel(4.2) app.

I want to update my database when the user is offline by the end of session (Automatic).

So I put the lifetime session to 1 minute for testing.

I create an event file :

<?php

Event::listen('auth.login', function($user)
{
    $user->is_online        = 1;
    $user->save();
});

Event::listen('auth.logout', function($user)
{
    $user->is_online        = 0;
    $user->save();
});

Everything works perfectly when I log in or log out manually but when I get automatically disconnected by the end of the session it does not work.

If you have an idea I would appreciate

Thanks.

我目前在laravel(4.2)应用程序中遇到问题。 p>

我希望在用户离开会话结束时更新我的​​数据库(自动)。 p>

所以我将生命周期会话设置为1分钟进行测试。 p> \ n

我创建了一个事件文件: p>

 &lt;?php 
 
Event :: listen('auth.login',function($ user)
 {  
 $ user-&gt; is_online = 1; 
 $ user-&gt; save(); 
}); 
 
Event :: listen('auth.logout',function($ user)
 {\  n $ user-&gt; is_online = 0; 
 $ user-&gt; save(); 
}); 
  code>  pre> 
 
 

登录时一切正常 或者手动注销,但是当我在会话结束时自动断开连接时它不起作用。 p>

如果您有任何想法,我将不胜感激 p>

谢谢。 p> div>

You cannot detect when a user closes their browser or navigates off your site using PHP

Instead, your best bet is most likely to store each user's last activity time.

Create a column in your user table along the lines of 'last_activity'. Whenever a user loads a page, update their last_activity to the current time. To get a list of who's online, just query the database for users with last_activity values more recent than 10 min or 20 min.

you can set session time using this

$_SESSION['time'] = time();

You have to detect if the session has ended by comparing it with the current time. For instance:

if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60))
{
   // Session expired
   Event::fire('session.expired');
}

Hook into the session.expired event and do whatever you want.