Skip to main content
CafeWebmaster

Main navigation

  • Tools
    • MD5 converter
    • SHA1 converter
    • Base64 encode
    • Base64 decode
    • HTML entities encode
    • HTML entities decode
    • Raw url encode
    • Raw url decode
    • UTF8 encode
    • UTF8 decode
    • Unix Time to Human Date converter
    • Remove duplicate lines
    • Sort text lines
    • Backwards - Mirrored Text
    • PiRaTe tExT
    • Your IP Address & Hostname
  • Forums
  • Free PHP Scripts
    • Image watermark with PHP
    • Page generation time with PHP
  • CSS
    • CSS hidden left menu
    • CSS horizontal menu
  • Free Fonts
  • Image Editors
  • Tutorials
    • Check password strength / safety with PHP and Regex
    • Cómo hacer que su sitio web popular!
    • How2 make a website popular
    • How2 secure your website
    • Kennwort Sicherheit mit PHP und Regex Prüfen
    • Page generation time and http-referrers with php
    • Redirect webpages with HTML, PHP, .htaccess, Java+Script, CGI-Perl, ASP.NET and ColdFusion
  • New
User account menu
  • Log in

Breadcrumb

  1. Home

Page generation time and http-referers with PHP

By php5, 19 May, 2009

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.

  • Log in to post comments

vinod kumar (not verified)

14 years 4 months ago

thanks for this useful posts.

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

CafeWebmaster.com(CW) 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.