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 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post

    Display The Names From Online Users

    Lets say i have 10 users in my database.
    I want to display 2 dynamic tables, one with the names from online users and one with the names from offline one..
    Any suggestion i am a bit confused

  • #2
    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
    What are you doing to determine which users are online/offline in the first place?
    If you are using predefined PHP sessions, this is a horrendous task. You need to first drag a list of files that are not considered expired, then you need to open them one at a time and parse them for information (and despite looking like it's serialized, its not the same algorithm used for the serialize/unserialize methods, so you literally need to write a custom parser).
    The other approach is to use a database driven session. You can chain it to the session_set_save_handler function's and you can issue on the write method an additional field for the username. You can't pull it directly out of the data unless you write the necessary decoding for it (up to three actually since you get the default, the binary and the wddx encoding options). So if you store the username in the session data, on top of the data provided to the write method you must also pull from the $_SESSION the existing username. That way you can put it along with a datetime for the last activity into the database. Now you can query based on time.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post
    i dont think i really understand what are u talking about..

    Atm i have a simple log in form and then the main page in which with a simple session i display the user's name.

    I suppose i should have a table "online users" in which i temporary store the data there..

  • #4
    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 garevn View Post
    I suppose i should have a table "online users" in which i temporary store the data there..
    Yes, effectively. The problem is controlling it; how can you tell that a user is now gone?
    For that, check into the session_set_save_handler, which allows you to write code to handle the sessions completely at the database level.
    The only troublesome part is that you must extract from the $_SESSION superglobal instead of just from the data during the write function. With the exception of the wddx there is no built in way to unserialize the data to pull it out (well, you can use session_decode, but it simply dumps into $_SESSION instead of returning an array which would be more useful). Not that this is a huge deal, but still.
    Essentially the problem is simple: PHP sessions are not designed to be interacted with except for a single session. So collecting information about many sessions takes an approach that requires a lot of manual handling.

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post
    i read the easy way is to add a field to my main table with 0/1 values 0 for offline and 1 for online. but how i can update the table when i someone login.. is it possible?

  • #6
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    I know of a more.. entry-level method. (With all respect to Fou-Lu)

    You create a simple script that you can put on each page, let's see if I can remember it all..

    First you check if a user session exists, or something that lets you know the page is being accessed via someone you want to log as online. If it is, you then take that user's username / id /whatever you want to use, and put it in the table for online users with a timestamp. Make sure you also allow for that user already being in the table for online users, in which case you would only want to update the timestamp not create another field.

    The second step is with displaying the online users, where-ever that might be on your site (This part does not have to be on each page!). You get the results of the online user table, which may or may not contain outdated results, and compare the timestamp that was last updated when they loaded a page (This is what the first step is doing) with the current timestamp. You can choose your own span of time for which they can be considered active, for example you may compare for timestamps that are 15 minutes old or less, or 10 minutes or less. Make sure you delete results that have become outdated in this moment as well, because although it would still only (providing it's working right) display the users that are current, you don't want the database to fill up with junk that is no longer needed.

    For this, if you are unfamiliar with dealing with times in PHP, you might want to research that a little bit. Hopefully this is quite understandable though.


  •  

    Posting Permissions

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