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 18.104.22.168 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.
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.
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?
You can use the $_SERVER[‘REMOTE_ADDR’] to get the visitors IP address. As people have said its unreliable and also you might need to consider privacy concerns with tracking it.
If more than one person from the same IP address connects, you can probably drop a cookie to flag you counted it, so if they come back you know they have been counted. Obviously, if they delete it, then there is no way of knowing if they already visited your website.
What kind of logic do you need for counting e.g. total unique IP addresses per month?