-1

Possible Duplicate:
timed auto logout and browser close

i am storing the user usage of a website in a mysql table and i am able to capture and store the login time using a insert query at the time of login-in and i am capturing the logout time also if the user choose to logout via logout button.

but what if the user just closes the browser then how can i fire the query for the logout time.

what i got till now is this, but with this i can address the session timeout issue but not the "browser close"

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) 

   {

    // last request was more than 30 minates ago
      session_unset();     // unset $_SESSION variable for the runtime 
      session_destroy();   // destroy session data in storage

   }

  $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

please help me to solve this out..

Community
  • 1
  • 1
Aditya Kumar
  • 793
  • 1
  • 8
  • 14

5 Answers5

4

There is no reliable way to know that a user has closed the browser or navigated away from your site to somewhere else - this is because of the stateless nature of HTTP which means once content is downloaded, there is no longer a connection between the server and the browser (for the most part).

You could possibly use some ajax request to "ping" your server - once that ping drops, you can assume they've close all pages to your site. This is bad, because it uselessly adds traffic to the network, and means that your server will be handling a lot more requests for no real gain in function.

HorusKol
  • 8,375
  • 10
  • 51
  • 92
2

One less resource using and also less relaying process can be use browser beforeunload event.

Send the ajax request to the server on browser beforeunload event and make the insert of user logout time.

A nice answer by SLAKS on How to capture the browser window close event?** is

The beforeunload event fires whenever the user leaves your page for any reason.

For example, it will be fired if the user submits a form, clicks a link, closes the window (or tab), or goes to a new page using the address bar, search box, or a bookmark.

You could exclude form submissions and hyperlinks (except from other frames) with the following code:

var inFormOrLink;
$('a').live('click', function() { inFormOrLink = true; });
$('form').bind('submit', function() { inFormOrLink = true; });

$(window).bind("beforeunload", function() { 
    return inFormOrLink || confirm("Do you really want to close?"); 
})

The live method doesn't work with the submit event, so if you add a new form, you'll need to bind the handler to it as well. Note that if a different event handler cancels the submit or navigation, you will lose the confirmation prompt if the window is actually closed later. You could fix that by recording the time in the submit and click events, and checking if the beforeunload happens more than a couple of seconds later.

Community
  • 1
  • 1
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
1

You can't track the closing of the browser. You can track pop up or close of tabs.

You can't rely on beforeunload. It's not compatible with browsers, I think.

ramse69
  • 11
  • 1
-1

This is what I tested and it worked for me when the browser is closed

session_start();
    if (!isset($_SESSION['LAST_ACTIVITY'])) {
        // initiate value
        $_SESSION['LAST_ACTIVITY'] = time();
    }
    if (time() - $_SESSION['LAST_ACTIVITY'] > 1800) {
        // last activity is more than 10 minutes ago
        session_destroy();
    } else {
        // update last activity timestamp
        $_SESSION['LAST_ACTIVITY'] = time();
    }

Here I am storing the timestamp of each acitivity of the user. When that time is more than 30 minutes ago, I doing a logout( destroy the session ) .

Not sure whether your application permits this but as far as I know this is the way I used to achieve in these kind of scenarios

EDIT after looking at comments

Closing the browser does not trigger any request to the server, so there is no way you can know that the user closed his browser. You can use a listener to way for the session to time out and then store the current time when that happens

You can talk to server when browser is closed by doing an ajax call on window.onbeforeunload OR window.onunload

My solution above will allow you to automatically logout the user when the time expires after some activity

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
-2

When the user closes the browser, the session is destroyed anyway. So you dont have to destroy it.

FrediWeber
  • 1,099
  • 1
  • 10
  • 21
  • i am not looking to destroy it, i am looking to store that time of time of destroy for that particular user into my table to analyse the usage. – Aditya Kumar Oct 08 '12 at 11:52
  • Then you have to use JavaScript and find a way to call a script when the user close the window. – FrediWeber Oct 08 '12 at 11:54