online users list

Posted on 2005-04-18
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
    LVL 1

    Accepted Solution

    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

    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

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

    Assisted Solution

    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 :
    LVL 3

    Expert Comment

    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
    LVL 9

    Expert Comment

    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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now