I want display number active connection in my website and write by PHP
How can I do?
Please help me!
How to Display number active connection in a website!
Moderator: General Moderators
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
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
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
The code I grabbed from Evilwalrus that I still use to this day.
MySQL Table Layout:
It's running script:
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)
);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");
}
?>