Page generation time and http-referers with PHP

Submitted by php5 on Tue, 05/19/2009 - 23:57

Webmasters want to know where all visitors coming from, which pages they visit and how long does it take webpages to load in miliseconds. This tutorial shows how to save your visitors/website details like referrer, browser/client, ip-address, langind-page in three steps. Remember if your scripts ends with exit() or die() command, footer code will not work. Just remove that commands or put them below footer code.

  1. Create your mysql table
  2. Add top-code
  3. Add bottom-code

SQL Query

Execute this code with phpMyAdmin or a similar tool to create your mysql table.

  CREATE TABLE IF NOT EXISTS `monitoref` (
  `id` int(11) NOT NULL auto_increment,
  `dtime` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `pg` double NOT NULL,
  `url` varchar(255)  NOT NULL,
  `ref` varchar(255)  NOT NULL,
  `ip` varchar(15)  NOT NULL,
  `browser` varchar(120)  NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;




Start code at the top of page

Put this code at the top of your .php file (config.php rewrite-catcher etc)

define("STARTMICROTIME", array_sum( explode(' ', microtime() ) ) );




End code at the bottom of file

Add this code to your last .php file, in most cases footer.php. Be sure that no exit() or die() command placed above this code.

$page_gen_time =  round(array_sum( explode(" ", microtime())) - STARTMICROTIME, 2);
$url = mysql_real_escape_string($_SERVER[REQUEST_URI]);
$ref = mysql_real_escape_string($_SERVER[HTTP_REFERER]);
$browser = mysql_real_escape_string($_SERVER[HTTP_USER_AGENT]);
$ip = mysql_real_escape_string($_SERVER[REMOTE_ADDR]);

    mysql_query("insert into monitoref ( pg, url, ref, ip, browser)
                values
                ('$page_gen_time', '$url', '$ref', '$ip', '$browser')");

Now you can analyze your visitors and page generation time with phpMyAdmin.

Add new comment