Over a million developers have joined DZone.

Detect Online Users With PHP

·
// You must have seen on some sites displaying the number of users online, this scripts exactly does that it displays number of users currently browsing your site.


 ";
}
$delete = mysql_query("DELETE FROM useronline WHERE timestamp<$timeout",$db);
if(!($delete)) {
print "Useronline Delete Failed > ";
}
$result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'",$db);
if(!($result)) {
print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);


mysql_close();
if($user == 1) {
print("$user user online\n");
} else {
print("$user users online\n");
}
?>


//The table structure 

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
); 
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}