Every month, thousands of people search for “website visitor counter HTML code.”
Most are building personal sites, student projects, or small-business pages and want a simple way to show how many people have visited.
I’ve built visitor counters for dozens of projects. Some use plain JavaScript, others use PHP backends, and a few use third-party widgets.
The approach you choose depends entirely on what you need.
This guide covers four methods, from a basic client-side counter you can build in 5 minutes to a server-side PHP solution that actually tracks real visitors across all browsers.
I’ll also cover free counter services and no-code tools for people who want results without writing a single line of code.
Build trust & FOMO
Highlight real-time activities like reviews, sales & sign-ups.
What Is a Website Visitor Counter?

A visitor counter is a widget that tracks and displays the number of visitors to your website.
Some people call it a hit counter or page view counter.
These counters typically sit in the footer, sidebar, or header of a web page.
They show a number that increases every time someone loads the page.
Behind the scenes, a visitor counter needs three things: something to detect page loads (usually JavaScript), a place to store the count (localStorage, a file, or a database), and a way to display the count on the page (HTML).
The simplest counter tracks page views per browser. More advanced ones track unique visitors across all devices using server-side storage.
If you’re running a business website, visitor counters also double as social proof.
Showing “1,247 people visited today” builds trust and creates urgency.
Method 1: JavaScript Visitor Counter with localStorage

This is the quickest way to get a working counter. It uses JavaScript and your browser’s localStorage to track visits. No server required.
Here’s the complete code you can copy and paste:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Visitor Counter</title> <style> .counter-box { text-align: center; padding: 20px; font-family: Arial, sans-serif; } .counter-number { font-size: 48px; font-weight: bold; color: #333; } </style> </head> <body> <div class="counter-box"> <p>Page views:</p> <span class="counter-number" id="count">0</span> </div> <script> let count = localStorage.getItem('page_view'); if (count) { count = Number(count) + 1; } else { count = 1; } localStorage.setItem('page_view', count); document.getElementById('count').innerText = count; </script> </body> </html>
How it works: When someone loads the page, the script checks localStorage for a stored count. If one exists, it adds 1. If not, it starts at 1. The updated number displays on the page instantly.
Limitations: This counter only tracks visits in a single browser. If someone clears their browser data, the count resets. And it doesn’t track visitors across different devices or browsers.
Best for: Learning projects, personal websites, or prototypes where you need a quick visual counter without backend setup.
Method 2: PHP File-Based Visitor Counter
If you need a counter that tracks ALL visitors (not just one browser), you need server-side code.
PHP with a simple text file is the easiest way to do this.
Here’s a working PHP visitor counter:
<?php $file = 'counter.txt'; // Create the file with 0 if it doesn't exist if (!file_exists($file)) { file_put_contents($file, '0'); } // Read current count $count = (int) file_get_contents($file); // Increment by 1 $count++; // Save the new count file_put_contents($file, $count); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP Visitor Counter</title> </head> <body> <p>Total visitors: <strong><?php echo $count; ?></strong></p> </body> </html>
How it works: Every time someone loads the page, PHP reads a number from counter.txt, increments it by 1, saves it back, and displays the updated count. Because the file is hosted on the server, every visitor (regardless of browser or device) is counted.
Setup steps:
1. Save the code above as index.php
2. Upload it to any PHP hosting (most shared hosting supports PHP)
3. The script automatically creates counter.txt on first load
4. Make sure the directory has write permissions (755 or 775)
Limitations: File-based storage can cause issues on high-traffic sites if multiple visitors load the page at the exact same millisecond. For most small to medium sites, this works perfectly fine.
Best for: Small business websites, portfolio sites, or any project hosted on a PHP server that needs real visitor tracking.
Also check: How to add a website visitor counter to create urgency
Method 3: Using a Free Visitor Counter Service

Don’t want to write code at all? Free counter services give you an embed code that handles everything.
Here’s the process:
1. Go to a service like FreeVisitorCounters.com, FreeCounterStat.com, or WebESTools.com
2. Pick a counter style (digits, colors, layout)
3. Copy the generated HTML/JavaScript embed code
4. Paste it into your website’s HTML, usually inside the <footer> or a sidebar section
Example embed code (from a typical free service):
<!-- Paste this in your HTML file --> <div id="visitor-counter"> <script type="text/javascript" src="https://www.freevisitorcounters.com/en/home/counter/123456/t/0"> </script> </div>
Popular free counter services:
FreeVisitorCounters.com offers multiple counter styles with no registration required. Counts are stored on their servers, so they persist across browsers.
FreeCounterStat.com provides basic analytics alongside the counter, including geolocation data and browser stats.
GoatCounter is an open-source, privacy-friendly option that provides a simple visitor counter API. You can self-host it or use their free hosted version.
Pros: Zero coding required, persistent counts, works immediately.
Cons: May show third-party branding, limited design control, adds external scripts that could slow your page, and you depend on their servers staying online.
Best for: Bloggers, hobby sites, and anyone who wants a working counter in under 2 minutes.
Method 4: No-Code Visitor Counter with WiserNotify

If you’re running a business website, ecommerce store, or a SaaS landing page, a basic number counter probably isn’t enough.
You want something that looks professional, tracks real data, and actually helps convert visitors.
WiserNotify’s live visitor counter does exactly that. Instead of showing a raw number in your footer, it displays real-time visitor data in clean notification bars:
“23 people viewed this page in the last 24 hours.”
“5 users just signed up.”
“81 visitors are online right now.”
The setup takes about 3 minutes. Install the tracking pixel, pick your widget style, configure what data to show, and go live. No coding, no server setup, no maintenance.
It also includes other social proof tools, such as recent purchase notifications, sign-up alerts, and countdown timers, that work alongside the visitor counter.
Best for: Business websites, ecommerce stores, landing pages, and SaaS sites where visitor data needs to drive conversions, not just display a number.
Which Method Should You Use?
Here’s a quick comparison to help you pick:
| Method | Tracks all visitors? | Needs server? | Coding required? | Best for |
|---|---|---|---|---|
| JavaScript + localStorage | No (single browser) | No | Basic | Learning, prototypes |
| PHP + text file | Yes | Yes (PHP hosting) | Moderate | Small business sites |
| Free counter service | Yes | No | None | Blogs, hobby sites |
| WiserNotify | Yes (real-time) | No | None | Business, ecommerce |
If you’re a developer learning HTML and JavaScript, start with Method 1.
If you need real tracking on a PHP site, go with Method 2.
If you want zero setup, pick Method 3 or 4, depending on whether you need basic counts or conversion-focused social proof.
5 Mistakes to Avoid with Visitor Counters

1. Using a counter that never updates
I’ve seen sites with counters stuck at “000123” for months. That’s either broken code or a static placeholder.
A frozen counter does the opposite of social proof. It makes your site look abandoned. Always verify your counter is actually incrementing by testing with a fresh browser session.
2. Placing it where nobody sees it
Burying a visitor counter at the very bottom of a long page means most visitors never see it.
If you’re using the counter as social proof, put it near key decision points: above the fold, near CTAs, or in a sticky header/footer. If it’s just for your own tracking, placement matters less.
3. Loading heavy scripts that slow your site
Some free counter services inject large JavaScript files or load tracking pixels from slow servers.
I’ve tested counters that added 200ms+ to page load time. Run your page through Google PageSpeed Insights before and after adding any counter script. If it hurts performance, switch to a lighter option.
4. Counting your own visits
If you repeatedly test and refresh your page, your counter inflates with your own visits.
The localStorage method is especially vulnerable to this. For PHP counters, you can add logic to exclude your IP address. For no-code tools like WiserNotify, filtering is built in.
5. Using client-side counters for real analytics
A localStorage counter is not analytics. It doesn’t tell you unique visitors vs. returning visitors, traffic sources, or conversion rates.
If you need real data, pair your visual counter with a proper analytics tool like Google Analytics or a dedicated reporting platform.
Build trust & FOMO
Highlight real-time activities like reviews, sales & sign-ups.
Wrapping Up
Building a visitor counter in HTML is one of the first projects most web developers tackle.
And it still works as a practical tool for showing site activity and building trust.
For learning purposes, the JavaScript localStorage counter gets you started in under 5 minutes.
For real visitor tracking, the PHP file-based approach handles the job on any shared hosting plan.
For a zero-effort setup, free counter services or tools like WiserNotify let you have a working counter without writing a single line of code.
Pick the method that matches your skill level and goals. And remember: a visitor counter is only useful if it shows real, accurate numbers.
Fake or broken counters do more damage than having no counter at all.