online members / guest's

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
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

online members / guest's

Post by spamyboy »

I need to creat such think - how ? should I count all members online, then members sessions and minus them from all (to get only guest).
Myabe someone has some snippet's / tutorials about this - any help will be helpful.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

You can do following.
1. Write a script, which insert a session_id to DB if it's not present there or update timestamp otherwise.
2. Call this script every X min. (sec.) with AJAX for example
3. Count of online users = count of DB records which were update during the last X min.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Got snippet of this kinde of script that use ajax ?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Script is very simple - if session_id is found in the table update time field with current time, if no add it. I don't think I should write PHP code here.
AJAX part is also very simple. Use can find a lot of examples is Google.
So you just need to use setTimeout() JS function
http://developer.mozilla.org/en/docs/DO ... setTimeout
Put the call of your AJAX request there and that is all.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Or, even simpler, don't use AJAX at all. The only time that it matters how many people are online is when someone wants to look at it, so only update it on page loads.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

superdezign wrote:Or, even simpler, don't use AJAX at all. The only time that it matters how many people are online is when someone wants to look at it, so only update it on page loads.
Yes it's possible but in this situation there will be another definition of "online user".
For example. I open some thread with 15 posts and read it for 30 minutes.
I won't be online for other users who just open a page.
Last edited by Gente on Fri Jun 22, 2007 8:35 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

If your session expires, you're not online anyway.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Yes. You're right.
Ok, let's just notice that this is 2 different ways for different logic.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yezzir. ^_^

I always see the "non-AJAX" way, mostly because I've never used the XmlHTTP thing before. Maybe eventually, but not yet.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Thank you superdezign and you to Gente,
but I rather chose non-AJAX way. :)
Post Reply