PHP Page Counter for website using a flat file and file handlings without database

0saves

Hi, in this post I am sharing a page counter which is most commonly used by us as a web developer in our day to day life. This page counter is not done with the help of database but it is done with the help of flat file. A flat file named count.php is there which contains the counter reading and an additional page to handle it and to increment the value everytime some visits the page.

This is very simple and easy page counter which counts all the hits.

The disadvantage is that it does not count the unique hits.

So there are two files involved in it.

count.php

{code type=codetype}

21

{/code}

counter.php

{code type=codetype}

<html>
<head>
<title>Page Counter with image as number</title>
</head>
<body>
<div align=”center”>
<p>&nbsp;</p>
<?php
$fileread = fopen(“count.php”,”r”);
if ($fileread)
{
$cnt = fread($fileread,1000);
fclose($fileread);
//Image Conversion
$len = strlen($cnt);
echo “You are visitor number <br><br>”;
for($i=0;$i<$len;$i++)
{
echo “<img src=’images/”.$cnt[$i].”.jpg’>”;
}
//======================
$file = fopen(“count.php”,”w”);
if ($file)
{
$cnt++;
fwrite($file,$cnt);
fclose($file);
}
}
?>
</div>
</body>
</html>

{/code}

You can download the source code here

Keep visiting my blog and please dont forget to share it with your friends.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related posts:

  1. How to get the URL of the Current Page using PHP Server Variables
  2. How to retrieve Alexa rank of any website using php
  3. CSV file upload and save it to MySQL database using PHP Code

About the Author: Abhishek Sanghvi

Hi Friends, my name is Abhishek Sanghvi and I am the founder of this “open source script” site MYPHPSCRIPTZ. I have been learning and practising PHP from the last 3 years. You could always contact me if you wish to have some code for your need. I would surely try and solve them for you at abhishek(at)myphpscriptz(dot)com

3 Comments + Add Comment

  • ha, I am going to experiment my thought, your post bring me some good ideas, it’s really amazing, thanks.

    - Norman

  • I should digg your post therefore other people are able to see it, very useful, I had a tough time finding the results searching on the web, thanks.

    - Murk

  • Hello dude! Thanks for sharing this code. I was really having a hard time figuring out how to do it. And I just learned right now that flat file can also be used in PHP because we just used that thing when we were having C++ discussion. This is a big help to me. Thanks much again!
    lieza@flat file cabinet´s last [type] ..Vintage Flat File Cabinet – A Useful Office Accessory

Leave a comment


seven + = nine

CommentLuv badge