Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I display the number of users currently on my website?

PHP & MySQL

How do I display the number of users currently on my website?

by  cLFlaVA  Posted    (Edited  )
This script will show you how to display the number of users that are currently browsing your website. It is intended to be used with a PHP website that communicates with a MySQL database.

There are three main aspects of this functionality: (1) the MySQL table, (2) the PHP function, and (3) the call to the PHP function.


(1) Creating the MySQL Table - the following SQL code will create the table you need for this functionality:
Code:
CREATE TABLE users_online (
  timestamp int(15) NOT NULL default '0',
  ip varchar(40) NOT NULL default ',
  PRIMARY KEY (ip)
) TYPE=MyISAM;

(2) The PHP Function - the following PHP code will perform the necessary INSERTS, DELETES, and UPDATES with the users_online table.
Code:
// returns number of users currently on the site
function getUsersOnline() {
	# Database-specific information:
	$server = "localhost";
	$database = "db_name";
	$user = "user_name";
	$pw = "password";

	# connect to the database
	$connection = @mysql_connect ($server, $user, $pw) or die ("Error connecting to the database.");

	# select the database
	$db = @mysql_select_db ($database) or die ("Error selecting database.");

	# time limit for users online in seconds - you can increase or decrease this if you want
	# recommended: entries in db older than 1 minute will be removed
	$timeout = 60;

	# get the ip address
	$ip = getenv("REMOTE_ADDR");

	# get the time
	$timestamp = time(); 
	$timelimit = $timestamp - $timeout;

	# delete expired users
	$sql = "DELETE FROM users_online WHERE timestamp < $timelimit";
	$result = @mysql_query ($sql);

	# insert current user
	$sql = "INSERT INTO users_online (timestamp, ip) VALUES ('$timestamp', '$ip')";
	$result = mysql_query ($sql);

	# if ip already exists, update the timestamp
	if (mysql_errno() == 1062) {
		$sql = "UPDATE users_online SET timestamp = '$timestamp' WHERE ip = '$ip'";
		$result = mysql_query ($sql);
	}

	# get number of users online
	$sql = "SELECT COUNT(*) as num_users FROM users_online";
	$result = mysql_query ($sql);
	$row = mysql_fetch_array($result);
	$num_users = $row['num_users'];
	
	if ($num_users == 1)
		return "1 user";
	else
		return $num_users . " users";
}

(3) The Call to the PHP Function - in order to display the number of users online, you must call the function. Place this code in any page where you would like to display the number of users that are currently on your site.
Code:
<? echo getUsersOnline(); ?>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top