Would you like to see how many hits your website or webpage have? Yes! I also wonder to see aswell.
I have a demo directory where I upload my portfolio type work to showcase. Very recently uploaded a pricing table built for a Genesis Framework project.
Share on bookmarking sites, and I got 2067 hits. So, let’s get the ball rolling to see how I added visitor counter PHP script on my page.
Add Visitor Counter to Website in PHP
We will need two files. One for the script and another for hits storage. Here is a screenshot of my directory. Sharing, so, you get the things quickly.
Step 1: Create a file named counter.php. Edit it with Notepad/Notepad ++
$handle = fopen("counter.txt", "r"); if(!$handle) { echo "could not open the file"; } else { $counter =(int )fread($handle,20); fclose($handle); $counter++; echo"Number of visitors to this page so far: ". $counter . "" ; $handle = fopen("counter.txt", "w" ); fwrite($handle,$counter); fclose ($handle); }
Save the file and upload it to the directory.
Step 2: Create another file named counter.txt. Upload this file to the directory. You can put any number in this file it will echo as a log where you want to show the page hits.
Step 3: You see the index.php file to that directory in the screenshot above that the folder where I am going to include the counter script. So we can see the “Number of visitors to that page.”
I wanted to add the visitor counter at the footer. So I wrote the below snippet under of all elements.
<div class="visitor-count"> <?php include "counter.php"; ?> </div>
How to Display Post Views in WordPress
Go to your WordPress Admin Dashboard » Plugins » Add New.
Search for Post Views Counter, install and active the plugin.
Now, go to Settings » Post Views Counter » Display
There you have all the available options to display post view counter for post or page type, position and more.
Conclusion
We have reached the end. To give your page a decent hits open counter.txt file add value/number and save it. That’s all! If you face any restriction, don’t hesitate to add your comment below.
You will love the following tutorials:
- How to Add Nofollow Tag to a Certain Domain
- How to Combine a Background Image and CSS3 Gradient on the Same Element
- How to Popup Genesis eNews Extended Opt-in Form
- How to Add Custom Logo to Genesis Child Theme
- 11 Genesis Framework Hacks for Beginner
- How to Use HTML Form [PHP]
- Starting with WordPress Metafields
- How to Store XML Data to PHP Variable
- How to Create Awesome Input Range Slider
- How to Make HTML Form Work and Send Mail
10 thoughts on “How to Add Visitor Counter to Website in PHP”
Thank you for posting. That may be helpful.
What’s this?! Please use a DATABASE!!
This is open-source. You can technically use DB.
BACOT AJG
thank you for the great counter script, unfortunately do not work for me i get a blank screen, pls advise and thank you in advance
Can you send me your URL?
dear sir/madam, thank you so much for the php script on add visitor counter . unfortunate displayed blank when i run the htm file as below. if i run php it displayed, i wanted the visitor counter in my webpage
New Page 1
It will work only in PHP file.
Hi does it count 2 if I visit the website for 2 times?
Thank you very much. It is very easy to understand