phpipvisitors

fetching unique ip address of computers on an internet network in php


I'm trying to build a database with records of unique visitors to a particular page on my website by obtaining their IP.

The code below fetches the visitor's IP:

$getip = $_SERVER['REMOTE_ADDR'];
echo "<b>IP Address: $getip</b>";

But I realize that this is very unreliable and inaccurate if various internet users are on a specific internet network and they all share the same IP address using different computers (Like a public cyber cafe or see example below).

Example:

XYZ Ltd uses a specific internet connection, if all the employees of XYZ Ltd are connected to the internet via the company internet connection and XYZ Ltd's IP Address is 69.18.107.24. If some of the employee of XYZ visits my page, the code above would not give me unique IP address of the user's computer rather I'll keep getting the company IP address (69.18.107.24) which is inaccurate and deceptive cos the application would see it as the same visitor whereas there are different visitors using the internet connection IP.

How can I fetch unique user IP based on user's computer regardless of whether they are sharing the same internet connection.

I dont want to use PHP cookies cos some browsers do not support it while some users disable it or somthing.

Would be very grateful getting help with this...Thanks!


Solution

  • It is not possible to get the local IPs of computers behind a shared connection. Even if you could get them, they would not be unique, because they are reused - that's the whole point of NAT. You would have a lot of people using 192.168.1.1 or similar addresses.

    If you want to differentiate between multiple machines that share the same IP address, you can add another factor, like the user agent string that tends to differ.

    For example:

    <?php
    md5(
        $_SERVER['REMOTE_ADDR'] .
        $_SERVER['HTTP_USER_AGENT']
    );
    // 80b567b7c7ccfbda75a9712f16ca4429
    

    would give you a hash that would only be identical if two users share the same IP address and have the same browser and operating system version.

    Visit Panopticlick to find out more about "browser fingerprinting". Surprisingly, browsers are almost unique in the combination of their configuration. If you bring in client-side analysis with Javascript, where you can access display resolution, installed plugin versions and other details, you can easily differentiate between users that have a shared IP.