online users list

Posted on 2005-04-18
Medium Priority
Last Modified: 2008-02-26
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
Question by:HamidHossain

Accepted Solution

reason100 earned 1000 total points
ID: 13809739
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.



Author Comment

ID: 13810036
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

Author Comment

ID: 13810069
You can see my question in this page to see the answers I got for javascript solution:

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

alextr2003fr earned 1000 total points
ID: 13810915
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 : http://cgiirc.sourceforge.net/
example of a page using such kind of onclose events : http://webchat.helioschat.org/irc.cgi

Expert Comment

ID: 13823473
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

Expert Comment

ID: 13826937
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.
LVL 14

Expert Comment

ID: 15990314
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question