iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 9, 2008
Form Field Hints
EarthWeb.com
 
May 8, 2008
Meet The Hardy Heron: What's New in Ubuntu 8.04
OReilly Network
 
May 8, 2008
Does Enterprise Development Have to Be Painful? (Part Two)
OReilly Network
 
May 8, 2008
Perl Pragma Primer
WebReference.com
 
May 8, 2008
1 comment
.net
 
May 7, 2008
Poll: Do you check the download speed of the pages you build?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

How Many Users Online? 

  Views:    7894
  Votes:    8
by Ben Sinclair 11/29/03 Rating: 

Synopsis:

Wanting to display how many users are on your website or on a certain page? Here's a simple script that will do just that...
Pages: 
The Article

You can easily show how many users are online with PHP and MySQL! Follow this quick tutorial:

The MySQL Part

First of all you will need to create a table in your MySQL Database:

CREATE TABLE `useronline` (
  `timestamp` int(15) NOT NULL default '0',
  `ip` varchar(40) NOT NULL default '',
  `file` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`timestamp`),
  KEY `ip` (`ip`),
  KEY `file` (`file`)
) TYPE=MyISAM;
 

Showing The Users

You now need to create a file called useronline.php. Fill out the configuration with your own information:

<?
// -------------------------------------
//  Configuration
 // -------------------------------------

$dbhost = "localhost";
$dbuser = "";  // MySQL Username
$dbpass = "";  // MySQL Password
$dbname  = ""; // Database Name
$timeoutseconds  = 300;   // How long till it will remove the user from the database(In seconds)

// -------------------------------------

$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
// Connect to MySQL Database
mysql_connect($d_host,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("Unable to select database");
// Add this user to database
mysql_query("insert into useronline values('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("<b>MySQL Error:</b> ".mysql_error());
// Dlete users that have been online for more then "$timeoutseconds" seconds
mysql_query("delete from useronline where timestamp<$timeout") or die("<b>MySQL Error:</b> ".mysql_error());
// Select users online
$result = mysql_query("select distinct ip from useronline") or die("<b>MySQL Error:</b> ".mysql_error());
$user = mysql_num_rows($result);
// Select users on this very page
$resulta = mysql_query("select distinct ip from useronline where file='$PHP_SELF'") or die("<b>MySQL Error:</b> ".mysql_error());
$usera = mysql_num_rows($resulta);
mysql_close();

// Show all users online
if ($user==1) {echo"$user user online!</font>";} else {echo"$user users online.";}
// Show users on this very page
if ($usera==1) {echo"<br>$user user viewing this page!</font>";} else {echo"$user users viewing this page.";}
?>
 

 

Now you need to include it on everypage:

<?php
include("useronline.php");
?>

That's everything! Enjoy!

Pages: 

Similar/related articles:


 
  Sponsors