Results 1 to 4 of 4

Thread: Sql table for users online help

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Smile Sql table for users online help

    Hello everyone,

    When a person logs into the member area on my site, their username and the time they logged in, is added to a database called online. From this, I can get a list of everone who is online at the moment. When they log off, their username is deleted from the table. The only problem is if the person dosen't log off before exiting my site, it will look as if they are still online. I would like a way to remove any user who hasn't been active for so many minuts(how do you spell it?) from the table. I'm sorry if I didn't explain it very well. Any help would be great!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are two methods:
    1. Run a cron job (linux, or similar method on windows, etc) every 5-15 minutes and delete any entries older than X minutes.
    2. Skip the cron job and just run that delete query every time anyone loads the page, when you log that individual.

    (2) is easier and should be fine:
    Code:
    mysql_query('LOG CURRENT USER');
    mysql_query('DELETE FROM `online` WHERE `time`<'.time()-(15*60).';');
    That assumes you are using unix timestamps and that you want entries older than 15 minutes to be deleted. In fact, I think 15 minutes is fairly common. You could do something lower (for example, 5 minutes), but that might mean that users just have not refreshed the page for 5 minutes. Of course they will be "online" again the next time they do load a page.

    Of course this is a bit excessive because it will run every time any user loads any page, but it does not take many extra resources from the system and will guarantee an accurate table always.


    The best way to think of this for static pages is that users are "online" if they have been recently online in the last few minutes-- such as 15. That doesn't mean they are still online, but they have been around recently. The situation would be different for a chatroom, for example, but since there is no accurate way unless they are constantly reloading the page (as in a chatroom) then you can't be certain about this with static pages.

    For an example, the forum here works like that. I think it's 15 minutes too.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Woudn't I have to put this on everypage to stop the users that are still online from being deleted?


    mysql_query('DELETE FROM `online` WHERE `time`<'.time()-(15*60).';');
    mysql_query("UPDATE `online`SET `time`= 'time()' WHERE `username`='$username42' ") or die(mysql_error());


    would that work?
    Last edited by keyboard; 09-21-2011 at 11:29 PM.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes, it is best to place this on every page-- otherwise you will just have a list of people who are on one page. Also, it won't matter which order you use the statements because the time between them will never be more than 15 minutes.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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