Adding a PHP pageview counter to every web page


What I am trying to accomplish is a simple PHP pageview counter for each of my PHP pages, I know this can be accomplished with MYSQL database as well, but I just want a simple page view counter with strictly PHP (Which I already accomplished).

I already found a script that works, but it only displays views for one page, I was wondering, if there was a way to edit the code so that with each page I include the counter, the number of views would be different for that page.

The website I found the instructions to follow is

And this is my code, that I edited to my needs:

function pageview_counter()
    if (isset($visitor)) {
        if ($visitor == "visited")
    } else {
        $file = fopen("pageview-counter.txt", "r+");
        $result = fread($file, filesize("pageview-counter.txt"));
        $result += 1;
        $file = fopen("pageview-counter.txt", "w+");
        fputs($file, $result);

And for the counter:

<p class="num-of-views">Views: <strong><?php echo pageview_counter(); ?></strong></p>

I created the text file in my website directory as directed, I just want to know if what I want can actually be accomplished. Any help will be greatly appreciated!


As commented before, a MySQL database would be the better option here, but if you insist on using a file instead, I would go with a custom INI file:

page1 = 0
page2 = 0
page3 = 0

You can add as many pages as you want and change their names of course. Just save that as pageCounts.ini. Next you’re going to need to be able to read and write to this ini file. I suggest creating a seperate PHP file for this and include it in the pages:


// Parse the ini file (Read)
$ini = parse_ini_file("pageCounts.ini");

// Save to ini file (Write)
function write_php_ini($array, $file = "pageCounts.ini")
    $res = array();
    foreach($array as $key => $val)
            $res[] = "[$key]";
            foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
        else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
    safefilerewrite($file, implode("\r\n", $res));

function safefilerewrite($fileName, $dataToSave)
{    if ($fp = fopen($fileName, 'w'))
        $startTime = microtime(TRUE);
        {            $canWrite = flock($fp, LOCK_EX);
           // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
           if(!$canWrite) usleep(round(rand(0, 100)*1000));
        } while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));

        //file was locked so now we can store information
        if ($canWrite)
        {            fwrite($fp, $dataToSave);
            flock($fp, LOCK_UN);



Source: How to read and write to an ini file with PHP

Save it as iniOperations.php and include it in the pages where you need a counter. Change page1 to the page where you’re including the counter.


// Include iniOperations.php if it isn't included yet
require_once "iniOperations.php";

// Show the current number of visits:
echo "Number of visits to this page: ". $ini["page1"];

// Increase the number of visits by 1 and write to ini file



Answered By – icecub

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published