/    Sign up×
Community /Pin to ProfileBookmark

online & offline status

hi there, i am working on a ladder competition website:

[url]http://www.xgs-gaming.com[/url]

as you can see here:

[url]http://www.xgs-gaming.com/standings.php?standings=online[/url]

players have their online and offline status, but the online status goes offline only if they press the logout button , which they hardly do.. >_< how can i fix a nice online offline status which is pretty accurate?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JesseBSmithJan 31.2010 — Without restating, take a look at the number once answer from the site below:

http://stackoverflow.com/questions/1051895/whats-the-easiest-way-to-determine-is-a-user-in-online-php-mysql
Copy linkTweet thisAlerts:
@dk_zero-coolJan 31.2010 — Easy

You just add online users to an online table, or use an isOnline column in a user table. Then on every request you make a database query deleting (Or set isOnline to false) every user that has been inactive for a certain amount of time (I always use 2 minutes). After this query, you put the user that requested the page in the online table, or update the timestamp if he or she is already in there.

For timestamp, use a dateTime column and use MySQL's TIMESTAMPDIFF(), or use an int column and compare PHP's time() output.

I's quite simple, and only takes up a few lines of codes. By storing the session ID in a unique column, you can do it with only to query lines using "ON DUPLICATE KEY", no php needed (Accept for the database functions/class of course)
Copy linkTweet thisAlerts:
@inseiauthorJan 31.2010 — ok thank you very much both. i will try to fix this using time functions, thanks to you.
Copy linkTweet thisAlerts:
@inseiauthorFeb 03.2010 — im having a little bit trouble achieving this.. is it possible to get some code examples perhaps? =/
Copy linkTweet thisAlerts:
@inseiauthorFeb 07.2010 — ok i managed to fix this now, thx.
Copy linkTweet thisAlerts:
@inseiauthorFeb 07.2010 — i just did it like this, it seems to work...

[CODE]function update_status () {
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
mysql_query("UPDATE ladder_users SET StatusTime='".time()."' WHERE Username='".$_SESSION['Username']."'; ");
}

$check_time = mysql_query("SELECT * FROM ladder_users;");

while ($row = mysql_fetch_array($check_time)) {

$timediff = time() - $row['StatusTime'];

if ($timediff<=300) {
mysql_query("UPDATE ladder_users Set Status='Online' WHERE Username='".$row['Username']."';");
}
else {
mysql_query("UPDATE ladder_users Set Status='Offline' WHERE Username='".$row['Username']."';");
}
}
}[/CODE]
Copy linkTweet thisAlerts:
@dk_zero-coolFeb 07.2010 — It can be done much better. You don't need to loop the whole table with PHP.

[code=php]function update_status () {
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
mysql_query("UPDATE ladder_users SET StatusTime=CURRENT_TIMESTAMP(), Status='Online' WHERE Username='".$_SESSION['Username']."'");
}
mysql_query("UPDATE ladder_users SET Status='Offline' WHERE TIMESTAMPDIFF(MINUTE, StatusTime, CURRENT_TIMESTAMP()) > 2");
}[/code]


The if statement will update the time stamp on the current user.

The query in the bottom will set every user to offline, if they have been inactive for more than 2 minutes. Just edit the number "2" to the amount of minutes you wish to use.

But your "StatusTime" column needs to be a datetime column.

And put an "allow null" on it, or remember to assign it a value when a user is created. Some MySQL Servers does not ignorer a "not null" without a value.
Copy linkTweet thisAlerts:
@inseiauthorFeb 07.2010 — that sure was way more elegant! thx for the tips, really apriciated!
×

Success!

Help @insei spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...