If you want to add simple PHP visitor counter in your website then here is simple solution which can lead you to add simple PHP visitor counter for your website.
It will work for your whole website and will save your counting data in Text file. Which can easily be emptied or change.
Note that this counter will work as text file. This is not a database Counter. But you can take an idea for saving visitors counter in database demonstration.
Visitor Counter in PHP
- Create a text file called visitors.txt.
- Create another file called index.php.
- Paste the below code in it. Whenever file refresh 1 increment in the visitors.txt file.
$visitors = file_get_contents('visitors.txt'); $visitors = $visitors+1; file_put_contents('visitors.txt',$visitors);
Explanation of Visitor Counter in PHP
- First line of the code will get the content from the file. File_get_contents is built in function of PHP.
- Second line will do an increment in it. $visitor+1 means last visit and 1 plus in it.
- The third line will put that increment value in visitors.txt again. Means will write in file.
- If you want to print the content or visitor visit. echo the file content again. just like below code.
$visitors = file_get_contents('visitors.txt'); $visitors = $visitors+1; file_put_contents('visitors.txt',$visitors); $content = file_get_contents('visitors.txt'); echo $content;
Other Example of Visitor Counter in PHP
Here is the counter link which will guide you through out the session. Session are PHP built in facility to control the visitor in a time frame manner.
Wiki How describe how to make a Counter in PHP.