Home arrow PHP arrow Page 4 - Tracking Website Statistics with PHP

Building a Page - PHP

If you have a website, especially one that sells products or services, it can be useful to track the visitors to your site. This article explains how to write a program that will do just that.

TABLE OF CONTENTS:
  1. Tracking Website Statistics with PHP
  2. Analyzing the Recorded Data
  3. Visitor Specific Analysis
  4. Building a Page
By: Jacques Noah
Rating: starstarstarstarstar / 34
September 12, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So, let's build a PHP page to display this information. Create a new PHP document, call it index.php and add the following code:

Script: index.php

<?
include "trdbcon.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Untitled Document</title>
<style>
.{
font-size:12px;}
</style>
</head>
<body>
<strong>WebSite Statistics</strong>
<br />
<table width="100%" border="1" cellspacing="1" bordercolor="#FF0000">
  <tr>
    <td width="16%"><strong>No. Visitors </strong></td>
    <td width="24%"><strong>Date Visited </strong></td>
    <td width="25%"><strong>Browsers Used </strong></td>
    <td width="35%"><strong>Pages Visited </strong></td>
  </tr>
  <?
  $query = "SELECT *,COUNT(*) FROM stattracker GROUP by ip";
$result=mysql_query($query);
$number_of_views = mysql_num_rows($result);
if($number_of_views>0){
while($row=mysql_fetch_assoc($result)){ 

  ?>
  <tr valign="top">
    <td><?=$number_of_views;?></td>
    <td><?=$row['date_visited'];?></td>
    <td><?=$row['browser'];?></td>
    <td><?=$row['page'];?></td>
  </tr>
  <? }
  }else{
   ?>
  <tr><td><p>No Results</p></td></tr>
  <? }?>
</table>
<br />
<strong>Visitor Statistics</strong>
<br />
<table width="100%" border="1" cellspacing="1" bordercolor="#FF0000">
  <tr>
    <td width="16%"><strong>Visitors ID </strong></td>
    <td width="24%"><strong>Date Visited </strong></td>
    <td width="25%"><strong>Browser Used </strong></td>
    <td width="35%"><strong>Pages Visited </strong></td>
  </tr>
   <?
 $queryV = "SELECT *,COUNT(*) FROM stattracker WHERE ip =
'127.0.0.1' GROUP by page";
$resultV=mysql_query($queryV);
$visitors = mysql_num_rows($resultV);
if($visitors>0){
while ($rowV =mysql_fetch_assoc($resultV)) {
//echo "Visited " .$row['count(*)']. " times";

  ?> 

  <tr valign="top">
    <td><?=$rowV['ip'];?></td>
    <td><?=$rowV['date_visited'];?></td>
    <td><?=$rowV['browser'];?></td>
    <td><?=$rowV['page'];?></td>
  </tr>
  <? }
  }else{
   ?>
  <tr><td><p>No Results</p></td></tr>
  <? }?>
</table>
<br />
<strong>Page Statistics</strong>
<br />
<table width="100%" border="1" cellspacing="1"
bordercolor="#FF0000" >
  <tr>
    <td width="16%"><strong>Page Name </strong></td>
    <td width="24%"><strong>No. Visits </strong></td>
    <td width="25%"><strong>Visited By </strong></td>
    <td width="35%"><strong>Referred from Page </strong></td>
  </tr>
  <?
  $queryP = "SELECT *,count(*) FROM stattracker WHERE ip =
'127.0.0.1' GROUP BY page";
$resultP=mysql_query($queryP);
if($visitors>0){
while ($rowP =mysql_fetch_assoc($resultP)) {
//echo "Page: " .$row['page']. "Viewed: ".$row['count(*)']. "<br>";
?>
   <tr valign="top">
    <td><?=$rowP['page'];?></td>
    <td><?=$rowP['count(*)'];?></td>
    <td><?=$rowP['ip'];?></td>
    <td><?=$rowP['from_page'];?></td>
  </tr>
  <? }
  }else{
   ?>
  <tr><td><p>No Results</p></td></tr>
  <? }?>
</table>
</body>
</html>

What this entire code does is display the three main sets of information as we've discussed previously. It is only a very general snapshot of who visited what page when and what browser they used. You even get the total number of times a particular page was visited.

Another area of analysis that I haven't discussed here is breaking the information down by the date of visit. You can for example run a query to find out the number of times a particular person visited in a month or year. There is just so much that you can do with the information that is collected by the script. There is no single best way of getting this information out, because it depends entirely on individual needs.

Create two more PHP pages and call them p1.php and p2.php.  Include the 'theCollector.php' page in both of them. In the p1 one, add a link to p2 and in p2 add a link to p1. These two pages are going to be our test pages.

Here's the code for p1.php. Use the exact same code when you create p2.php:

Script: p1.php

<?
include "theCollector.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p><a href="p1.php">page1</a></p>
<p><a href="p2.php">page2</a></p>
</body>
</html>  

Run p1.php, click on the link to p2.php and then click on the link to p1.php. Repeat this process a couple of times. This will populate the table with some data for us to use later on.

Conclusion

With the information that you get from the collector script, you can extract a lot more information than I did in this article. You can even use PHP's gd to give a graphical view of your web stats. But for most web masters, this should be adequate or act as a base to write more advanced web statistics scripts.



 
 
>>> More PHP Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: