Over a million developers have joined DZone.

How to Create a Simple PHP Text Counter

· Web Dev Zone
After learning the basics of PHP's basic file system functions, the first thing you'll want to do is put it to use. One of the easiest and flashiest things you can create is a page counting script. I'll show you how to create a page hit script that is easy to create and even easier to implement.
<?php
  // Part 1
     $filename = "counter.txt";
     $count = file_get_contents($filename);
     if ($count == null)
        $count = 0;
     echo $count;
  // Part 2
     $count++;
     $handle = fopen($filename, "w+");
     fwrite($handle, $count);
     fclose($handle);
?>

This is the entire script. Let's go through it line by line.

$filename = "counter.txt";

This assigns the filename to a variable to be used throughout the rest of the script. In this case I used counter.txt. The first thing to do after implementing this script is to create counter.txt on your server in the same directory as this script and chmod is to 777 so that PHP may write to the file later on. Here is a great tutorial on how to chmod files: http://support.discusware.com/center/resources/howto/chmod.html

 
$count = file_get_contents($filename);

This line stores in entire contents of counter.txt into the variable $count. In this case there is only a number inside counter.txt so $count holds that number. 

 if ($count == null)
       $count = 0;

If the file is empty we need to make a case for that or problems will come up later. If $count does not contain anything than we go ahead and set $count to 0.

 echo $count;

This will display the number inside the text file.  

 $count++;

This adds 1 to the current $count.

$handle = fopen($filename, "w+"); 

The next thing to do is to open the file for writing and to truncate it (erase its contents), that's why I choose w+. Please see my article on file system basics for an explanation on other modes. fopen() requires that we assign a resource handle to be referenced in the future.  

  fwrite($handle, $count);

This line writes the variable $count to our file represented by $handle.  

 fclose($handle);

This is the final line of the script where we do some cleanup. This closes the file we opened earlier. It isn't essential to the script what its always nice to cover all bases.

Now that you understand how the script works it's time to implement it.

 <?php

    include("counter.php");

?> 

 This assumes you name the script above counter.php. Wherever you put this in your page is where the script will echo the page hit count. It's as easy as that. 


Topics:

Published at DZone with permission of Mike Bernat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}