Web-development informational articles

Track your visitors, using php - web-development


There are many assorted travel breakdown tools, ranging from clear-cut counters to absolute transfer analyzers. Even though there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can certainly construct a log file surrounded by minutes. In this commentary I will show you how!

Getting the information

The most crucial part is in receipt of the in a row from your visitor. Thankfully, this is enormously easy to do in PHP (or any other scripting foreign language for that matter). PHP has a exceptional inclusive adjustable called $_SERVER which contains numerous location variables, plus in rank about your visitor. To get all the in sequence you want, austerely use the next code:

// Being paid the information

$ipaddress = $_SERVER['REMOTE_ADDR'];

$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";

$page . = iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");

$referrer = $_SERVER['HTTP_REFERER'];

$datetime = mktime();

$useragent = $_SERVER['HTTP_USER_AGENT'];

$remotehost = @getHostByAddr($ipaddress);

As you can see the bulk of in rank comes from the $_SERVER variable. The mktime() (http://nl2. php. net/mktime) and getHostByAddr() (http://nl2. php. net/manual/en/function. gethostbyaddr. php) functions are used to get bonus in a row about the visitor.

Note: I used a act in the above illustration called iif(). You can get this behave at http://www. phpit. net/code/iif-function.

Logging the information

Now that you have all the in a row you need, it must be in print to a log file so you can later look at it, and construct convenient graphs and charts. To do this you need a few down-to-earth PHP function, like fopen (http://www. php. net/fopen) and fwrite (http://www. php. net/fwrite).

The below code will first conceive a accomplish line out of all the information. Then it will open the log file in "Append" mode, and if it doesn't exist yet, build it.

If no errors have occurred, it will write the new logline to the log file, at the bottom, and at length close the log file again.

// Coin log line

$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . " ";

// Write to log file:

$logfile = '/some/path/to/your/logfile. txt';

// Open the log file in "Append" mode

if (!$handle = fopen($logfile, 'a+')) {

die("Failed to open log file");


// Write $logline to our logfile.

if (fwrite($handle, $logline) === FALSE) {

die("Failed to write to log file");



Now you've got a fully affair taking down module. To start tracking visitors on your website basically add in the classification module into your pages with the include() do (http://www. php. net/include):

include ('log. php');

Okay, now I want to view my log file

After a while you'll almost certainly want to view your log file. You can by a long way do so by basically using a banner text editor (like Notebook on Windows) to open the log file, but this is far from desired, since it's in a hard-to-read format.

Let's use PHP to engender constructive overviews for is. The first thing that needs to be done is get the inside from the log file in a variable, like so:

// Open log file

$logfile = "/some/path/to/your/logfile. txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r");

$log = fread($handle, filesize($logfile));


} else {

die ("The log file doesn't exist!");


Now that the log file is in a variable, it's best if each logline is in a branch out variable. We can do this using the explode() affair (http://www. php. net/explode), like so:

// Seperate each logline

$log = explode(" ", trim($log));

After that it may be advantageous to get each part of each logline in a break away variable. This can be done by looping by means of each logline, and using explode again:

// Seperate each part in each logline

for ($i = 0; $i < count($log); $i++) {

$log[$i] = trim($log[$i]);

$log[$i] = explode('|', $log[$i]);


Now the absolute log file has been parsed, and we're ready to start generating some exciting stuff.

The first thing that is very easy to do is in receipt of the add up to of pageviews. Austerely use count() (http://www. phpit. net/count) on the $log array, and there you have it;

echo count($log) . " associates have visited this website. ";

You can also cause a absolute overview of your log file, using a clear-cut foreach loop and tables. For example:

// Show a table of the logfile

echo '';

echo 'IP Address';

echo 'Referrer';

echo 'Date';

echo 'Useragent';

echo 'Remote Host';

foreach ($log as $logline) {

echo '';

echo '' . $logline['0'] . '';

echo '' . urldecode($logline['1']) . '';

echo '' . date('d/m/Y', $logline['2']) . '';

echo '' . $logline['3'] . '';

echo '' . $logline['4'] . '';

echo '';


echo '';

You can also use custom functions to filter out exploration engines and crawlers. Or build graphs using PHP/SWF Charts (http://www. maani. us/charts/index. php). The promise are endless, and you can do all kinds of things!

In Conclusion. . .

In this commentary I have shown you have to construct a taking down module for your own PHP website, using naught more than PHP and its built-in functions. To view the log file you need to parse it using PHP, and then demonstrate it in anything way you like. It is up to you to coin a kick-ass interchange analyzer.

If you still desire to use a pre-built passage analyzer, have a look at http://www. hotscripts. com.

About The Author

Dennis Pallett is a young tech writer, with much come across in ASP, PHP and other web technologies. He enjoys writing, and has on paper a number of articles and tutorials. To find more of his work, look at his websites at http://www. phpit. net, http://www. aspit. net and http://www. ezfaqs. com.

Web development: A gap in education  Tennessee Tech Oracle

What Is Web Design?  Technology Zimbabwe

The Future of Web Development  Business 2 Community

Developed by:
home | site map
goldenarticles.net © 2020