users online, sessions help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

users online, sessions help

Post by anthony88guy »

So I have a site, and I would like to display current users online. The current way I am accomplishing this is, I have a database with a sessionid, time, ip fields. Now when a user enters the site, a session is started and the sessionid, time and ip is entered into the database. I run a query selecting rows within the last 15mins. Sessions are destroyed when the browser closes.

My dilemma is that when you enter the site, and then end the session. Then re-visit the site (creating a new session) it counts that as 2 different users thus faking the users online count. Now what i could do is if the users ip = ip in database update that row, but what if you have multiple computers shared one ip?

One way I can think of but don't know how to create the query is select rows within last 15mins but with different ips.

Any ideas? Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

15 minutes is a large gap.

I'd suggest using ajax to fetch users who are online in a more realtime fashion. You can key it off of an ID on a row. It might show dupes, but if you lessen the time for a 'refresh' the dupes won't show as long.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Burrito wrote:15 minutes is a large gap.

I'd suggest using ajax to fetch users who are online in a more realtime fashion. You can key it off of an ID on a row. It might show dupes, but if you lessen the time for a 'refresh' the dupes won't show as long.
My site doesn't receive must activity so i kept it large. I rather not use AJAX when i know it can be done with PHP and MySQL.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could use a cookie then.

set it to expire in 15 mins....
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Burrito wrote:you could use a cookie then.

set it to expire in 15 mins....
I don’t see the need to add additional stuff (lack of better word), when it could be done without.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Look at the system that phpBB uses to count its users online. phpBB uses sessions for logged in users as well, so it may be inline with what you want.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you've already pointed out that there's no way to uniquely identify users. You can't key it off of the ip address because multiple people could use the same IP. You don't want the same user shown twice and if they return within the 15 minute timeframe, they would.

the only solution I can see is to key it with the ip address in conjuction with the username (decreases the room for error, but error is still potentially there), or to use a cookie (which would be 100% (assuming the user can and will accept cookies)).

so in response to your last post, given the 'tools' you're using, it can't be done...you're gonna have to add more tools to your bag to accomplish what you're after.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

with regards to Evarah's post:

phpBB uses a log in to generate the session, or a cookie (which essentially logs the user in again).

as I just said above, that's the only way you can do this.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Hey, that's what I do (sometimes). I have modified the phpBB code to my needs so it would do what the OP wants.
Post Reply