who is online

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
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

who is online

Post by irealms »

i am using a script to show how many users are online

Code: Select all

<?php
echo '<div class="log">';
$server = "localhost"; 
$db_user = "cadmin"; 
$db_pass = "cpass"; 
$database = "crimson"; 
$timeoutseconds = 300; 

$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

mysql_connect($server, $db_user, $db_pass); 
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 
} 
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout"); 
if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'"); 
if(!($result)) { 
print "Useronline Select Error > "; 
} 
$user = mysql_num_rows($result); 


mysql_close(); 
if($user == 1) { 
print("<b>$user</b> user online\n"); 
} else { 
print("<b>$user</b> users online\n"); 
} 
echo '</div>';


?>
i've tried a few things to get it to show who those users are but can't it it to show the names. Any ideas?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

/me is online :)


You have the ip adres of the users. If you inser a user_id of somekind in the database you can join that to a user database.
liquedus
Forum Newbie
Posts: 2
Joined: Thu Jun 05, 2003 9:00 am
Location: Ottawa, Canada
Contact:

Post by liquedus »

try this, you will be able to show users ip's:

<?php
echo '<div class="log">';
$server = "localhost";
$db_user = "cadmin";
$db_pass = "cpass";
$database = "crimson";
$timeoutseconds = 300;

$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

mysql_connect($server, $db_user, $db_pass);
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);


mysql_close();
if($user == 1) {
print("<b>$user</b> user online\n");
} else {
print("<b>$user</b> users online\n");
}
print "User IP's: <br /><br />";
while($r = mysql_fetch_object($result)){
print $r->ip.", ";
}
echo '</div>';


?>
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

thanks liquedus but i want to link this script to call out the username from the users table. So for each person that is on the site it will take their name from the user database.

Inter i see what you mean, just not sure how to go about it.
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

i have a session variable called valid_user, any way to use the names for each valid user and for the rest user 'guests = ' ?
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

Post by Little Spy »

im using this in one of my scripts you will need to adapt it of course but here is how I do it

Code: Select all

<?php
// Stuff For Users Online (its here so users online information is recorded on every page)
$timeout = 5*60; //minutes (* 60) until user 'times' out
$date = time(); //now
$time_minus_timeout = $date - $timeout; //how many minutes from 'now' they timeout

//find total people online now
$totalonline = rows_query("SELECT userid FROM online WHERE lastonline > $time_minus_timeout");


//total online ever
$data = fetch_query("SELECT value FROM misc WHERE name = 'mostonline'");
$mostonline = $data['value'];

if ($totalonline > $mostonline) {
	$totalonline_date = time();
	$query = reg_query("UPDATE misc SET value = '$totalonline_date' WHERE name = 'mostonline_date'");
	$query = reg_query("UPDATE misc SET value = '$totalonline' WHERE name = 'mostonline'");
}

$new = 1;
//if the user is logged in.. or else not logged in
if ($_SESSION['ib_userid']) {
	$query = reg_query("SELECT userid, ip FROM online WHERE lastonline > $time_minus_timeout");
	while ($data = fetchonly_query($query)) {
		if ($_SESSION['ib_userid'] == $data['userid']) {
		       $new = 0;
		       break;
		}
	}
	//insert him into the table
	if ($new == 1) $query = reg_query("INSERT INTO online (userid, name, lastonline, ip) VALUES ('$_SESSION[ib_userid]', '$_SESSION[ib_username]', '$date', '$_SERVER[REMOTE_ADDR]')");
	//the user might have been considered a guest so delete his guest <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> just in case
	$query = reg_query("DELETE FROM online WHERE ip = '$_SERVER[REMOTE_ADDR]' AND userid = '-1'");
} else {
	$query = reg_query("SELECT ip FROM online WHERE lastonline > $time_minus_timeout");
	while ($data = fetchonly_query($query)) {
		if ($_SERVER['REMOTE_ADDR'] == $data['ip']) {
		       $new = 0;
		       break;
		}
	}
	//insert him into the table
	if ($new == 1) $query = reg_query("INSERT INTO online (userid, name, lastonline, ip) VALUES ('-1', 'Guest', '$date', '$_SERVER[REMOTE_ADDR]')");

}

$guests_online = rows_query("SELECT userid FROM online WHERE userid = '-1'");

//create users online string
$query = reg_query("SELECT name FROM online WHERE userid != '-1'");
while ($data = fetchonly_query($query)) {
       $i++;
	if ($i != $totalonline) { 
		$online_string .= $data['name'].", ";
		$onlin_list .= $data['name']."<br>\n";
	} else {
		$online_string .= $data['name'];
		$online_list .= $data['name'];
	}
}

$members_online = rows_query("SELECT name FROM online WHERE userid != '-1'");

// Delete all the old ones
$query = reg_query("DELETE FROM online WHERE lastonline < $time_minus_timeout");
?>
?>
Post Reply