0

I have table of 'users' having 'is_login' column. When a user logged in. 'is_login changes to 1' AND when log out 'is_login changes to 0'.

But if user closes browser, that 'is_login' column stays 1.

I need a method to update database, when session expires by closing browser.

I have tried this but not done: How to exec a function when codeigniter session expires

I am open to other suggestions as well.

Community
  • 1
  • 1

1 Answers1

0

You would need to use some javascript/jQuery to catch when the browser is closed down and make an AJAX call to a script at your application that can set the logged in flag to 0. Using jQuery

$( window ).unload(function() {
  $.post('location/of/your/script.php',{'identifier':'identifier_val'});
});

Then in script.php pick up whatever you post and act on it.

This is pretty unreliable in my experience and not a great experience for the user as this will fire if they reload the page. The other (equally nasty) way would be to use the heartbeat method. Essentially your jQuery needs to ping your server every minute. Then you would run a CRON job to check any user records that did not get a ping from the jQuery for more than 2 mins (or whatever you think is correct) and set those users to logged out.

Otherwise you need to open a socket connection with your server which is the proper way to do it.

Mike Miller
  • 3,071
  • 3
  • 25
  • 32