online users list

I have a website that has an area for users who can login and make use of some services ... when the user logged in I register a session with his info. And I store a session expire time in the DB  for each user (15 min) ... when the user change the page I reset the expire time so if he does not move for more than 15 his session will be destroyed automatically and he will be logged out.

In another area I have to list the online users by listing the ones from my DB which their session expire times are not expired ...

if the user logged out or his session time expire he will not be listed any more in the online users ...
The problem occures if the user closed the window and leave the site ... his session will be destroyed but he will be listed in the online users until his session time ends.

I want to delete him also from the online users the moment he close the window if he did not  logout !

 Thank you
Who is Participating?
reason100Connect With a Mentor Commented:
I believe the only way to do this is to add some javascript to your pages that will call a php page with the session_destroy function when the browser window is closed. Here's a link to another Experts Exchange article on this.

HamidHossainAuthor Commented:
I did that through javascript as follows but I did not get a 100% correct result; sometimes it works other times it does not:

<script language="JavaScript">
function check_user(id)
 if((id != null) && ((window.event.clientX<0) || (window.event.clientY<0)))
   return true;
 return false;
$LogId = $id;

<body onUnload='return check_user(<? echo "$LogId"; ?>);'>

$id is the id of the user that is registered in his session ...
The above code worked with me but not in all cases; for example if I have more than one window opened for the same user account it does not work correctly and after closing thw windows the use still listed in the online users
HamidHossainAuthor Commented:
You can see my question in this page to see the answers I got for javascript solution:
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

alextr2003frConnect With a Mentor Commented:
did you try something like this :
<script language="JavaScript">
function do_quit(id) {
   var i = new Image();
   i.src = "deluser.php?del_id="+id;
   or just window.location="deluser.php?del_id="+id; // not sure here

<body onbeforeunload="do_quit()" onunload="do_quit()">
<img src="logout.jpg" onclick="if(confirm('Are you sure you want to quit?')){do_quit();parent.location=''}" title="Close">

the idea is to readapt the scripts from cgi chat systems wich disconnects you when you close the window
for more details check how this works :
example of a page using such kind of onclose events :
try this.

place hidden frame on the page.

let us say the file for that frame is logout.php

Keep your userid in session variable.

when you close your browser, that frame is called automatically.

In logout.php delete your user using session variable.

hope this helps
A very painful, and annoying way of doing this would be to make the hidden frame refresh every couple of seconds. If it fails to refresh, then they aren't in the window anymore.. that or your script has broken.

Anyway. In the hidden frame you could have an automatically refreshing page that updates a database, or a session (expiry time of 5 seconds or so?). As said earlier. If the page fails to refresh their session dies, or their time() stamp in the database is older than the 5 seconds.

The annoying thing is if the user has their "Start Navigation" sound on, they get a click every 5 seconds. If they don't support frames, they are never logged in, and cannot log in.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: reason100 {http:#13809739} & alextr2003fr {http:#13810915}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.