Skip to content Skip to sidebar Skip to footer

(PHP) Create hit counter without database

Visitor hit counter

Knowing how many user views there are on the page is sometimes very important. For example, if you want to separate popular articles, you definitely need to know the number of visitors. This time, I’m going to share a code that can show a hit counter or can be called a visitor counter. The code that I’m going to share this time around without having to use a database is certainly very simple.

However, if there are many on the search engine (google) who share the process of creating hit counters with a database, in this article it is the opposite, namely without a database. So use the help of a file with the extension .txt (text format) which is used to receive and display visitor hits. Basically, many also share such scripts, but can let the size of the .txt file grow large with the number of visitors, but don’t worry, because the PHP code I’m going to share is friendly on all servers, which of course doesn’t take time a lot of quota. Here is the code, please see below.

Just don’t you? the above code uses the function fopen (), fread () and fwrite () that can work to display content hits.txt, and increase the number with every visitor. The explanation is so simpler, when there are visitors to the site, the code immediately reads the contents of the numbers from the hits.txt file and adds the number 1 out of a number of hits that are present in each visitor. This is very easy to understand when you look at the contents of the code above.

It’s very easy to use, just copy and paste the code above and then create a file called hits.php and paste the file into the footer template, for example. Then create another file called hits.txt in the same directory as hits.php, then please look at the results.

The above code is very simple and cannot just display individual visitors. I made it simple on purpose so that it would be easy to learn. Those who are practiced can develop it even better. If you want to add something above / ask a question? Please comment.

Hopefully useful and good luck