A PHP code to count visitors per IP addresses

Hello,
I need a PHP code that counts the visitors of a page per IP address and just show unique visitors. For example, if a visitor with 1.2.3.4 IP address visited that page twice, then my PHP code count it as one visit.
I want to have a counter.php file and when browse that page, it show me a message like “Visitors: Number”.
I found a tutorial like https://html-online.com/articles/simple-php-mysql-visitor-counter/, but it uses MySQL Database and never count unique IP addresses.

Thank you.

Why would you want to restrict your count to a single count per IP address? Many businesses would present the same IP address for all their internet connections - our office, for example, came through a single router. If I found your web site and told all my colleagues about it, you’d never know they had been looking because they’d all show the same address.

In any case, break it down into steps:

First, write some code to find the remote IP address.
Then check your database and update accordingly.

If the person with IP address 1.2.3.4 visited yesterday and then closed down the browser and then visited again today presumably you would want that to only show a count of one? In which case you will need to store, somewhere, a record of the IP addresses that have visited. This could be done with a MySQL database. It may be, though, that you could use a file, using the fopen() function instead, and store the IP addresses in a file. I don’t know enough about PHP to know whether you could, or indeed should, do it this way. Or you could use cookies.

And, in the case of home internet connections (and probably phones, especially if they’re using public or roaming wi-fi connections), a lot of those have dynamic IP addresses - if I visit your site today, then come back next week, I may present a different IP address on those two visits.

That’s probably why very few people track this by IP address.

You could, but it won’t perform as well as a MySQL database would once the visitor numbers are high. It’s probably not an issue for appending the new IP address onto the end of the file, but remember that first you have to search that file to see if it’s already there.

Using a database table would make this very simple, just set the IP-address column as unique and attempt to insert the new one, then trap the “already-exists” error that you get. Or don’t, if the only thing you care about is tracking each unique IP address. Or you could use a single query to either insert the IP address or, if it already exists, update a count to show how may times each IP visited. As long as you don’t mind the inaccuracy of using IP addresses to count visitors.

I can tell you my cellphone has no less than 4 IP addresses in any given normal work day; I wake up on my home LAN, disconnect from it in the car on my way to work and get an IP from my provider, disconnect from that when i get to work and pick up the local employee LAN, disconnect from that on my way home and pick up another IP from the cell provider.

And all of that’s assuming the cell provider doesnt drop or rotate my IP somewhere during the journey.

IP address tracking is hugely unreliable. You could drop a cookie on the browser with a really long lifetime that basically says “I’ve been here already”, and only count people who havent got a cookie, though i don’t know how precise your definition of ‘unique visitor’ needs to be - am i a different visitor if I put my cell phone down and open my PC’s browser?

3 Likes