Page Hits Counter

Apply a page hits counter using PHP and HTML

Add a page hit counter to your website. The hit counter can be stored in a database or a file. A hit occurs whenever there is a request to a web server for a file.

Each file sent to a browser by a web server is an individual hit.

In this tutorial, every single page request will count as one hit.

    Tools are required:

  • Text editor for creating and modifying the page hits counter files.
  • Web server.
  • PHP.
  • HTML.
  • Knowledge of the files to be served.

Create page_hits.txt file

This is an empty file that is used to store page hits. Make sure the file has write permissions.

Page_hit.php file

<?php
/*
 *      page_hit.php
 *      
 *      Copyright 2010 Edward <http://ojambo.com/contact>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
/* Create an empty file called page_hits.txt 
 *  With permissions of 777 in same path
 * 
 *  USAGE:
 *  include ("page_hit.php");
 */
	//Define the file storing the counter
	$page_hits_file = ("page_hits.txt");
	
	// Counter variable and read file into string
	$page_hits = file_get_contents($page_hits_file);
	//Increment the counter (page hits) by one
	$page_hits ++;
	
	//Open the file and prepare it for writing
	$file_handle = fopen($page_hits_file , "w");
	fwrite($file_handle , "$page_hits");
	
	//Close the file
	fclose($file_handle);

	//Display the hits
	echo $page_hits;
?>

Define the page_hits_file as page_hits.txt. Read the page_hits.txt file into a string using file_get_contents. Increment the counter variable by one. Prepare the page_hits.txt file for writing using fopen, and write the updated counter using fwrite. Close the file using fclose and display the updated page hits using the echo command.

Index.php file

<!--
        index.php
        
        Copyright 2010 Edward <http://ojambo.com/contact>
        
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
        
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
        MA 02110-1301, USA.
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Ojambo.com Page Hits Counter Tutorial</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
	<h1>Ojambo.com Page Counter</h1>
	<p><?php include ('page_hit.php'); ?></p>
</body>

</html>

How to Use:

In the index.php file, utilize the include command to display the information from page_hit.php

    Recommendations:

  1. This is an introduction to website analytics.
  2. Please note, that hits are not a reliable measure of website traffic.
  3. In this tutorial, one page is used, but most CMS systems include several pages.

Tags: , , , , , ,

This entry was posted on Monday, September 20th, 2010 at 12:47 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One response to “Page Hits Counter”

  1. […] Video for the ojambo.com page hits counter article. Part One of […]

Leave a Reply

Your email address will not be published. Required fields are marked *