Page 1 of 1

unsure of players online script

Posted: Sat Dec 26, 2009 4:34 pm
by ZeroComp
not sure if this correct but I'll give it a go

insert this in the index wherever you want basically is what my idea was
first add a field to players called 'online' make it a boolean (true,false)

Code: Select all

$playerOnline = mysql_fetch_field('online'));
if ($playerOnline == true) {
echo "There are $playerOnline players online";
}
wondering if I'm on the right track or if I am dead on or completely off

Re: unsure of players online script

Posted: Sat Dec 26, 2009 4:59 pm
by Torniquet
the way i done it was to make a new table which stores a time stamp which gets updated everytime you change page.

then call this to see if a user is online or not on their profilepage or user list.

Code: Select all

$activeTime = time() - 300;

$sql = mysql_query("SELECT timestamp FROM user_log WHERE id='$_GET[id]'");
$active = mysql_fetch_array($sql);

if($active['timestamp'] < $activeTime){
	$activeDisplay = "../images/offline.gif";
} else {
	$activeDisplay = "../images/online.gif";
}
which shows a green image if they are online.

so if a user has been active in the last 5 minues they are classed as online. (i know its not the best, but am hoping to make it better :D)

i also use a similar script to display the amount of users online (within the 5 minutes) in the menu bar section,

Code: Select all

$onlinetime = time() - 300;
echo mysql_num_rows(mysql_query("SELECT timestamp FROM user_log WHERE timestamp > '$onlinetime'"));

then ontop of that i have in the footer a list of all active users.

Code: Select all

$getOnline = mysql_query("SELECT username, id FROM user_list WHERE email_active='1' ORDER BY username ASC");


$activeTime = time() - 300;

print "Current users online:<br />";

while($row = mysql_fetch_array($getOnline)){
	
	$check = mysql_query("SELECT timestamp FROM user_log WHERE id='$row[id]'");
	$active = mysql_fetch_array($check);
	$namecolour = mysql_fetch_array(mysql_query("SELECT name_colour FROM user_account WHERE id='$row[id]'"));
	
	if($active['timestamp'] > $activeTime){
		echo "<a href='../member/viewprofile.php?id=" . $row['id'] . "'><font color='$namecolour[name_colour]'>" . $row['username'] . "</font></a>, ";
	}
	
}

Hope that helps you in someway or another :)


edit for scrteenies:

Image

Image

as you can see in the 1st screenie. sable has got a green light next to the name (cuz its the account im logged into lol) and the others have no light.
the name also appears in the footer, and the active user count is displayed in the side bar. (the green name has no relevance to this lol, its a mod account :p)

in screenie number 2... you can also faf about with the code to display how long ago they were last online, and if there is no record for them on the log table, it is displayed as never.

Re: unsure of players online script

Posted: Sun Dec 27, 2009 4:46 am
by ZeroComp
cool :) at first I thought that was javascript and I was thinking whoa! way over my head there :lol:

Re: unsure of players online script

Posted: Sun Dec 27, 2009 5:03 am
by Torniquet
i have enough trouble with sendin xmlhttp requests lol. i try to stick to as little ajax as i can atm XD

if you need any help with it lemme know n im sure i can give ya a hand