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.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.