With PHP it is very easy to create a counter or a visitor counter. We can use all internal functions of PHP for this.
First we have to count all calls of the script and save them in a text file. We also make sure that each visitor/call is counted only once, for which we use the cookie function of PHP. Finally, we can output the current count everywhere using the file_get_contents function.
The big advantage of the counter is that no database is needed. We store everything in a simple text file located in the main directory of the website. Moreover, all the logic can be packed into a function and you can create counters with any name.
If you want to reset the counter, just remove the file "counter.txt" and the script will start counting the calls from the beginning. Note that you must inform visitors that you are setting cookies and what they are used for, as required by the Privacy Policy.
Was this article helpful?
Yes
0
No
About Vitali Lutz
Vitali Lutz is a versatile author on all aspects of business and technology. Thanks to his ability to adapt to a wide range of topics and his great thirst for knowledge, he writes about anything that interests him.
Redirection running... 5
You are redirected to the target page, please wait.