Home Advanced Code Users online - on your site

Users online - on your site

E-mail Print PDF
You must have seen this on some sites where they display the number of users online. This scripts exactly does that. It displays number of users currently browsing your website.

The table structure

Code:

 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)
  );

User's Online Displays the numbers of users on your website at any one time.

Code:

 <?php
  $server = "xxx";
  $db_user = "xxx";
  $db_pass = "xxx";
  $database = "xxx";
 
  $db = mysql_connect($server, $db_user,$db_pass);
  mysql_select_db($database,$db);
 
  $timeoutseconds = 300; //5 minutes
 
  $timestamp = time();
  $timeout = $timestamp-$timeoutseconds;
 
  $insert = mysql_query("INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')",$db);
  if(!($insert)) {
  print "Useronline Insert Failed > ";
  }
  $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");
  }
  ?>
 

Domain Names

Sample image .com .info
.mobi .net
.org .tv .us
.biz .ws .name
.am .at .be .cc .cn .de .eu
.fm .jobs .jp .ms .nu .nz
.tc .tw .uk .vg .ASIA .ME

Website Hosting

Sample image We're the affordable, reliable place to host your site. And unlike the competition, there's no set up fee and no annual commitment.

24/7 Support

Sample image Our highly trained, courteous support staff is waiting to help. We'll resolve any issue to your complete satisfaction.