View Full Version : Gather all user's logged in $_SESSION['user_id']

05-21-2010, 03:46 AM

I am creating a very VERY basic chat script, and i have spent some time trying to find the answer to this before posting but had no luck.

I would like to create a small column on the side to show logged in users.
How might i go about this or maybe point me in the direction of a tutorial?!

Thanks everyone!


05-21-2010, 03:57 AM
Chat scripts are very difficult because this requires that you indirectly connect all of the users to each other through the server, through ONLY page/data requests from the users. (The server cannot request information from the user, only the other way around.)

1. Sessions are ONLY available for an individual user. They're useless if you're trying to connect two users. For ME $_SESSION['username'] is MY name. For YOU $_SESSION['username'] is YOUR username, and there is no way to bridge the two.

2. If you need to connect this info, you will need to do two things:
i) Store the current info in a database, just like you will need to for the messages (text).
ii) Use math (like 1 minute of no activity = left room) to GUESS when a user is no longer in the room.

Or just find an existing chat room. There isn't much out there for PHP/MySQL, and you can actually find some nicer ones for other languages like Java applets, Flash, etc.

There are a couple decent scripts out there, though. The only one I've liked is an addition to SMF (simple machines forums) called "achat", but it only works within that forum. (You could try to extract it and the author is working on an independent version, but it would be a big undertaking or a while to wait.)

Due to the "real time connectivity" (at least in theory) of this, it's quite difficult to do.

05-21-2010, 04:48 AM
okay, so i would need to make a table 'active_user' and store:

user_id | time | active(0/1)<--true false

Then query the database for user's to see the logged in users. But, what if they don't logout.. then i would need to reference a script on every page header, let's say:


And in the active_user.php we will have a query that runs through the table 'active_user' and cleans up any rows that are older than '15min'. And just to be safe incase the person happens to be reading something on a page for more than 15min.., i will put a javascript on all common pages to renew the logged in users $row['time'] so they don't get accidentally cleaned out by the script running when someone else log's in. As long as their browser is open, they will stay active according to the table.


Where do i get this javascript to do that!! ---> off to the javascript forum!

Thanks Dan!!