PHP Page Hit Tracking Script

<<-back  |  start over

Step 3 - Tracking Page Stats

Now we're ready to finish this up. In order for your pages to be able to use PHP to track their hits, they all need to be saved as PHP pages. Usually, depending on your server, this just means they need to have the .php extension. When your pages are ready, open them and view the source code.

At the very top of the source code, above the opening <HTML> tag, make a new line and enter the following code...

<?php
$trackPage = "Page Name";
include("../scripts/stats/db.php");
include("../scripts/stats/TrackHits.php");
?>

For the first variable, $trackPage, change the value inside the double quotes from "Page Name" to whatever you want to appear as the name of the page that's opened. This is not the name of the file necessarily, but whatever you want to show up for name of the page in the stats pages.

The next two lines of code are just importing the necessary PHP files that handle the stats. The first connects to the file that establishes the database connection, and the second connects to the file that reads the hit stats and writes them into the database.

You'll need to change the text inside the double quotes to be the relative path to where those script files are in relation to the current page.

Repeat these steps on any pages (or even groups of pages) that you'd like to keep stats for. You can have more than one occurance of the above code if you wish to keep stats for the individual page and maybe the group of pages it's in, just give each block of code it's own $trackPage name, such as "Page Name" and "Group Name".

That should do it!

<<-back  |  start over