How to create visit counter in PHP

Every web master want to now and show the visit count of the website. There are many analytics tools available such as Google analytics but showing the count on the website is diferent thing. You can also use some 3rd party tools too. But to go for those tools if you can create it with simple coding.
In this tutorial, I will show you, how you can create a visit counter with PHP and a text file. You will also realise that this one is simplest way.
Create a php cage and a text file count.txt and put 0 in it.
Now put this code in php file

$count = file_get_contents("count.txt");
$count = trim($count);
$count = $count + 1;
$fl = fopen("count.txt","w+");

The php code is simple. It will open the txt file and then increment the value inside it by 1. Every time yo have a new visitor on your website, it will increment the value by one.

This script do not show anything on the webpage. You can show visit count on the webpage just by showing the content of the text file as code below

$count = file_get_contents("count.txt");
$count = trim($count);

You can customize he look of the output for better look.

This code does not count unique visits.

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.

Similar Articles


Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker