Anvar Logo
01 / 01
Anvar Logo +91 808 999 0405

PHP simple hit counter with session

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);
?>
by anvar
Published : Oct , 3
  1. That’s useful web for me..
    I added your web-site into my bookmarks!
    Keep up good work! Excited for future updates!
    Regards,
    Thomas

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories