Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts

    How to tell if a user is online

    I have a webpage where users login. What I am trying to do is create some kind of code so that I know when they are online and when they are offline.

    I am thinking about using a SQL db to store it, but I don't know the best way to get around doing this.

    Also, I want to stay away from onload and onunloads if possible, as this will cause slow page loading times every time the user goes to a different subpage. (but at this point I will take anything.)

  • #2
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    just check whether they have logged in or not

    make the flag field in user table which tells they are logged in or not

  • #3
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    yes, the only problem is is that if they just click 'X' instead of logout, the db will still say that they are logged in.

  • #4
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    When they log in set the session variable

    Code:
    $_SESSION["logged"] = true
    
    or if you want more detail set this variable to a value which uniqly identifies the user e,g
    userid
    If this variable has that value it means the person is online

    If user closes the browser then this variable will have no value

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    There's really no way to do this in "real time" but what you can do is, if the user is logged in, everytime they load a page, update a "last_action" field in the database. Then, you program your site to query the database for everyone with a last action within 5 minutes (or some other interval). This would be a good "rough" indicator of who's online.

  • Users who have thanked bcarl314 for this post:

    333qaz333 (01-23-2008)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by o0O0o.o0O0o View Post
    When they log in set the session variable

    Code:
    $_SESSION["logged"] = true
    
    or if you want more detail set this variable to a value which uniqly identifies the user e,g
    userid
    If this variable has that value it means the person is online

    If user closes the browser then this variable will have no value
    That is not accurate. When the user closes the browser the object in use (session) loses its 'pointer' to say, and the session is no longer retrievable. The session data itself remains on the server until the next garbage collection run by PHP.
    DB Driven sessions.
    Only way I can think of, similar to what BCarl was getting at. If you clean up your dead sessions after a given period of time, the count is easy.
    Alternative method:
    Iterate through every session file available (must be stored in a local directory), hope the garbage collection has not gotten to it, and check the last_activity (or something of the sorts) to determine if its still 'current'. Not fun. Trust me.

    Go with the DB Sessions, you can probably find a thousand tutorials through google relating to them.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    333qaz333 (01-23-2008)

  • #7
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    thank you two for your help. I am very greatful.

    Honestly, I don't know how to use session variable at all (I think they are like cookies?), so I think I will try the sql table method.

    Where can I learn about sessions?

  • #8
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    John

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by 333qaz333 View Post
    thank you two for your help. I am very greatful.

    Honestly, I don't know how to use session variable at all (I think they are like cookies?), so I think I will try the sql table method.

    Where can I learn about sessions?
    You are right, they are like cookies, sort of. Except they are stored on the server filesystem instead of the client system - this makes them more secure than cookies alone.
    Generally the two are mixed, a cookie is (by default) to be set on the client machine if it can, that holds a reference to the session on the server's filesystem
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Could you give me an example of using sessions in relevance to what I am trying to do?

    I am reading tutorials, but i am just not getting sessions yet...


    I get how to store the sessions on an individual browser, but how could i get it on the server, so I can tell who is online or not.

    I would like to do something similar to this site, where all users can tell who is online now.
    Last edited by 333qaz333; 01-23-2008 at 02:33 AM.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Mmkay.
    You would run against your standard sessions that you would use on a page, for example:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['lastActivity'] = time();

    ...
    ?>
    Now, add to it a location (or do it via .ini directly or using an .htaccess file if you have it available), relative to the calling script:
    PHP Code:
    <?php

    ini_set
    ('session.save_path''/my/path/to/session/storage');
    session_start();
    ...
    Now for the fun part. You got to count those sessions:
    PHP Code:
    <?php

    $i 
    0;
    foreach (
    glob('/path/to/session/storage/*') AS $sessions)
    {
        
    $i++;
    }
    Voila, simulated counter. This doesn't even keep track of the record time, for that you will need to unserialize the packed data in the session, read the 'lastActivity' hash value, determine if its still online or not, and increment the counter.
    Decided to edit this part in to give you an example for how to check to see if its a current session. Not sure if this will actually work how I expect it to, but will give it a shot:
    PHP Code:
    $i 0;
    foreach (
    glob('/path/to/session/storage/*') AS $sessions)
    {
        
    $sess unserialize($sessions);
        if (
    is_array($sess) && ($time array_key_exists('lastActivity'$sess)))
        {
            if (
    $time time() - TIMEOUTCONST)
            {
                
    // Still considered online
                
    $i++;
            }
        }

    As you can see, this isn't overly pleasant, and I don't want to throw a script together for this purpose >.<.
    DB's are a lot easier to deal with (but harder to put together until you get the hang of it). In case you are wondering, I use database sessions exclusively now, haven't used the predefined sessions in years (except to test on them and keep as 'up to date' as I can with them). To count off a DB driven session:
    PHP Code:
    $qryStr "SELECT count(*) FROM {SESSIONTABLE} WHERE `lastActivity` > time() - 3600";
    $numOnline $sqlObj->query($qryStr); 
    All done on that - obviously this is using a custom sql object and query itself won't do, but a simple fetch is all that is required on top of it. The 3600 is mearly a constant number to subtract, think of it as a 'timeout', anyone with no activity after 3600 seconds is considered to be disconnected and not included in the help.
    Forum software (like this site) use Database driven session management, thats why the counts are so easy to preform.
    Hope this helps you out, and I'm pretty sure I mentioned it already - just search google (or maybe even here not sure if someone has posted one) for a 'database session management in PHP' script or tutorial. There are a lot out there.
    Good luck
    Last edited by Fou-Lu; 01-23-2008 at 04:02 AM. Reason: Maybe I should show how to add it with the time on the files...
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    333qaz333 (01-23-2008)

  • #12
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Thank you very much.

    I guess I will stick with the DB method... I don't think I will mess with the session way.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •