You can set simply the hit counter as per the real user or web page visit. This script using with session so it will check the unique visitor of you website. If you want to count the page hits by reload the page you can also do it by removing the session.

For this working you need to create a hit file (hit.txt) in your script path.

If you want you can set the hits yourself 🙂

Page count with Session : unique visitors

<?php
error_reporting(0);
session_start();
if(isset($_SESSION['start'])){
if (file_exists('hit.txt')) {
$fil = fopen('hit.txt', r);
$dat = fread($fil, filesize('hit.txt'));
echo "Hit:".$dat;
}
}
else{
if (file_exists('hit.txt')){
$fil = fopen('hit.txt', r);
$dat = fread($fil, filesize('hit.txt')); 
echo $dat+1;
fclose($fil);
$fil = fopen('hit.txt', w);
fwrite($fil, $dat+1);
}
$_SESSION['start'] = "started";
}
?>

Page count with page load : for web page hits

<?php
$fp = fopen("hit.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
echo "<p>Page views:" . $count . "</p>";
$fp = fopen("hit.txt", "w");
fwrite($fp, $count);
fclose($fp); 
?>

Leave a Reply

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