Page 1 of 1

How to Display number active connection in a website!

Posted: Thu Jun 26, 2003 10:31 pm
by toantt
I want display number active connection in my website and write by PHP
How can I do?
Please help me!

Posted: Fri Jun 27, 2003 8:12 am
by AVATAr
afaik you do not "have" a connecction, you can create a session, and you can "count" the active sessions.

how to do that?, well in this forum there are a lot of solutions to that problem (try searching "number of active users", active sessions.. )

a snippet:
1- establish sessions with users (loggin an that)
2- use a DB to record them with some date or time
3- count that

Posted: Fri Jun 27, 2003 11:49 am
by phice
The code I grabbed from Evilwalrus that I still use to this day.

MySQL Table Layout:

Code: Select all

Create MySQL Table... 

# 
# Table structure for table 'useronline' 
# 

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) 
);
It's running script:

Code: Select all

<?php 

$server = "xxx"; 
$db_user = "xxx"; 
$db_pass = "xxx"; 
$database = "xxx"; 
$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"); 
} 

?>