"members online" script.

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
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

"members online" script.

Post by Mightywayne »

I've looked everywhere, and google won't help me. =/ The scripts are either too complicated (and thus too hard on the server, I need something basic) or part of this huge thing I'd have to dissect.

All I want is when people log in to my game, for it to tick them as signed on. 15 minutes of inactivity, they're logged off. I was previously using IPB forums, but I've sinced just opted to code my own forums, and now I don't have a way to check this.

I was thinking I'd have it set "15" when they log in, and then every minute, it takes 1 away from all users with > 0 minutes logged in, and when it reaches 0, it doesn't consider them logged in anymore. This means, however, every page I'd have to set (maybe in the footer) the counter = 15 with mySQL. A whole query a page is something I don't want to do, along with a big query every one minute.

What should I do?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: "members online" script.

Post by Zoxive »

Make a log in the database when the "User" logs on.

Code: Select all

SELECT count(id) FROM `logins` WHERE `born` >= DATE_SUB(born, INTERVAL 15 MINUTE);
Edit: Born is a poor example, last modified, or updated would be much suffice.
Last edited by Zoxive on Tue Feb 26, 2008 1:20 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: "members online" script.

Post by John Cartwright »

On every page, update the users last action time

Code: Select all

UPDATE users SET lastonline = NOW() WHERE id = $userid
Then basically do the exact same query as Zoxive to check for users that are currently online. Alternatively you can use ajax to poll the user every x seconds to determine if their browser is still open.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Re: "members online" script.

Post by Mightywayne »

Lol I'm not pro enough for AJAX yet, but thanks folks, that is a better way to do it.
Post Reply