End session on Browser Close

Joined
Oct 28, 2010
Messages
888
Reaction score
115
Hi guys, I've been making a website in PHP, it's fully functional and I've got the majority of it working.

I have a function for an admin to be able to see what user's are currently browsing the website with their details, again, that works perfectly.

But my problem is, when a user closes the browser, the session is still active and does not set the column where it states the user online to offline.

On my log-out button, I have this php code called:

PHP:
if(isset($_GET['logout'])) {
   mysql_query("UPDATE members SET status='N' WHERE mem_user='".$_SESSION['user']."'");
   session_destroy();
   echo '<meta http-equiv="refresh" content="1; url=index.php">';
}

I want to know how I can end a session and update the user's status column to go to 'N' when the user's session is no-longer active.

Many thanks,

Mathew Clarke
 
Hmm, you can consider saving the current timestamp along with the location so you can change the function for the administrator to select all records which are not older than x seconds.
 
Basically you add an extra column in the table and update the current timestamp (PHP: time - Manual) everytime you update the current location of the user.

Then you change the function in the backend to select all records which are not older than current time - timestamp in database > timeout in seconds.

Maybe someone has got a better solution though, never too late to learn. :)
 
Basically you add an extra column in the table and update the current timestamp (PHP: time - Manual) everytime you update the current location of the user.

Then you change the function in the backend to select all records which are not older than current time - timestamp in database > timeout in seconds.

Maybe someone has got a better solution though, never too late to learn. :)

Okay, thank mate,I'll look into it, if anyone has another solution - please let me know! ;D
 
Another option is to connect via websockets and mark a user as logged off when they navigate away from the page.

tcp websockets are not common nowadays. in case of polling its possible but very uncomfortable. I rather use polling with AJAX to receive data into my callbacks without reloading pages. :)
 
It's pretty common, actually. And you can use socket.io to have it gracefully fallback on swfs or longpolling.

Very common indeed, only today did I find out that the deployment and bug tracking system "Springloops" which I have been using everyday for the past few months actually uses socket.io to display when an action takes place to keep you updated without the need for refreshing. Also keeps in sync perfectly when a user is online or displaying the same ticket as you.

I would definitely suggest looking into socket.io for any real-time user tracking stuff.

All the best,
Richard Komakech.
 
It's pretty common, actually. And you can use socket.io to have it gracefully fallback on swfs or longpolling.

oh swfs and socket.io. i wasnt refering this. i was talking about real websockets browsers can use, not any js library that starts to simulate websocking. i bet socket.io uses new websocket technologies on new browsers that support it, while socket.io simulates socket technology by using polling on old browsers. the "real" websocket module of browsers is not common and only supported by new browsers.

whatever, thanks for the tip about socket.io, but since im working with aspx, i would build a webservice and use the simple ways to do both, websockets, and if no websocket is possible cuz of an old browser: polling.
 
oh swfs and socket.io. i wasnt refering this. i was talking about real websockets browsers can use, not any js library that starts to simulate websocking. i bet socket.io uses new websocket technologies on new browsers that support it, while socket.io simulates socket technology by using polling on old browsers. the "real" websocket module of browsers is not common and only supported by new browsers.

whatever, thanks for the tip about socket.io, but since im working with aspx, i would build a webservice and use the simple ways to do both, websockets, and if no websocket is possible cuz of an old browser: polling.

socket.io uses websockets if it's available, and support for websockets is already widespread:

Can I use Web Sockets

The only major browser that doesn't support it is opera mini, which will soon be a modified version of Chrome, and so that will also support it.

socket.io falls back at the base case on longpolling, which is better than basic polling anyway.
 
socket.io uses websockets if it's available, and support for websockets is already widespread:

Can I use Web Sockets

The only major browser that doesn't support it is opera mini, which will soon be a modified version of Chrome, and so that will also support it.

socket.io falls back at the base case on longpolling, which is better than basic polling anyway.

yes, as i thought. but u cannot just code for only major browsers always. there are many stupid users who doesnt care to update. =(
so its a good think it falls back to polling in this case. however, i rather use a ms webservice use such technology. :P
 
Back