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.
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.