Page generation time and http-referers with PHP

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.

  `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)
                ('$page_gen_time', '$url', '$ref', '$ip', '$browser')");

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


thanks for this useful posts.

thanks for this useful posts. Really, i need this!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <pre> <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <style><img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • You may insert videos with [video:URL]

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image. is a free online community for webdevelopers and beginners. Anybody can share their code, articles, tips, tutorials, code-examples or other webdesign related material on the site. Newbies can submit their questions and reply to existing questions. CW does not guarantee or warrant reliability of code, data and information published on the site. Use the site on your own risk. The site takes no responsibility of direct or indirect loss or any kind of harm to its users. The site also doesn't take responsibility of infected files or source code with any kind of infection or viruses, worms, spywares, malwares, trojan horses. CW reserves the right to edit, move, or delete any of content for any reason.