Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2012, 05:14 PM   PM User | #1
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
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
garevn is offline   Reply With Quote
Old 12-24-2012, 05:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-24-2012, 06:04 PM   PM User | #3
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
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..
garevn is offline   Reply With Quote
Old 12-24-2012, 07:08 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-24-2012, 07:40 PM   PM User | #5
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
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?
garevn is offline   Reply With Quote
Old 12-24-2012, 08:23 PM   PM User | #6
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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.
Custard7A is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:11 AM.


Advertisement
Log in to turn off these ads.