There are many different traffic analysis tools, ranging from simple counters to complete traffic analyzers. Although there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can easily create a log file within minutes. In this article I will show you how!
Getting the information
The most important part is getting the information from your visitor. Thankfully, this is extremely easy to do in PHP (or any other scripting language for that matter). PHP has a special global variable called $_SERVER which contains several environment variables, including information about your visitor. To get all the information you want, simply use the following code:
Now that you have all the information you need, it must be written to a log file so you can later look at it, and create useful graphs and charts. To do this you need a few simple PHP function, like fopen (http://www.php.net/fopen) and fwrite (http://www.php.net/fwrite).
The below code will first create a complete line out of all the information. Then it will open the log file in "Append" mode, and if it doesn't exist yet, create it.
If no errors have occurred, it will write the new logline to the log file, at the bottom, and finally close the log file again.
Now you've got a fully function logging module. To start tracking visitors on your website simply include the logging module into your pages with the include() function (http://www.php.net/include):
include ('log.php');
Okay, now I want to view my log file
After a while you'll probably want to view your log file. You can easily do so by simply using a standard text editor (like Notepad on Windows) to open the log file, but this is far from desired, because it's in a hard-to-read format.
Let's use PHP to generate useful overviews for is. The first thing that needs to be done is get the contents 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));
fclose($handle);
} 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 separate variable. We can do this using the explode() function (http://www.php.net/explode), like so:
// Seperate each logline
$log = explode("
", trim($log));
After that it may be useful to get each part of each logline in a separate variable. This can be done by looping through 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 complete log file has been parsed, and we're ready to start generating some interesting stuff.
The first thing that is very easy to do is getting the number of pageviews. Simply use count() (http://www.phpit.net/count) on the $log array, and there you have it;
echo count($log) . " people have visited this website.";
You can also generate a complete overview of your log file, using a simple 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 search engines and crawlers. Or create graphs using PHP/SWF Charts (http://www.maani.us/charts/index.php). The possibilities are endless, and you can do all kinds of things!
In Conclusion...
In this article I have shown you have to create a logging module for your own PHP website, using nothing more than PHP and its built-in functions. To view the log file you need to parse it using PHP, and then display it in whatever way you like. It is up to you to create a kick-ass traffic analyzer.
Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several 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.
The Best Way To Give Medicine To A Baby You have a spoon full of medicine for baby in one hand, and you're holding baby in the other. So how do you get the spoon in her mouth?!
Coaching Prime Time An awful lot of fantastic coaching has been coming out of Hollywood lately, have you noticed?
Raising Issues In Your Group or Offline Drawing on his experience with clients as they've tried to deal with emotionally-sensitive topics in their groups, Matt offers three principles to help you deci...
An Overview of the Gatlinburg Cabins Originally founded in the early 1800s as White Oaks, Gatlinburg was just a sleepy residential town until the growth of its lumber industry in the early 1900s. T...
Use Real-Life Templates For Writing Success At some point along the way, most of us have used what are
commonly called "fill-in-the-blank" writing templates. We
might have used them to write a letter, for...
Why Clean Mobile Homes for a Business? Manufactured and Mobile Home owners know that the weather this year will be quite harsh. It's imperative these owners to clean out rain gutters and debris from...
Weight Loss with Hoodia Gordonii Hoodia gordonii is believed to contain the natural appetite suppressant property. Some studies have reported its hunger suppressant effect.
A Guide to Home Dehumidifiers It can be difficult to choose from among the countless home dehumidifiers out there. First, determine the square footage of the largest room you plan to dehumid...
The Hated Cellulite Cure That Works How do women in the advertisements for cellulite creams get rid of their cellulite? Not by using a cream, of course. One may answer that Adobe Photoshop editing...
10 Ways To Gain An Avalanche Of Sales 1. Utilize holidays to increase your visitors or sales. You could give away free electronic greeting cards, hold discounts, send customers holiday cards, etc. 2...
Internet Marketing - Article Announcer Reflection on being a newbie in this business to the today in the life of an internet marketer. An introduction to ArticleAnnouncer.
How to Create an Interest Story for the Press If you think you have something that is interesting and you could bring attention to your company because of it, then you need to read through this article. Thi...
A Day in Your Life: 17 Ways to Make it a Special Day Life brings a muliplicity of emotions, events, defeats, and victories. Each day is a brief and precious time to experience its vast range of emotions and react ...
White Sun - The Spiritual Test A piece of twisted and decayed wood cannot be used for carving. A wall made of loose mud cannot be painted. Test is only given to those who are qualified. Jesus...