Questions with IP function

Asked

Viewed 37 times

0

I use the following function below to search the user’s IP, regardless of the type of connection or device it uses.

  function get_client_ip() {
$ipaddress = '';
if ( isset( $_SERVER[ 'HTTP_CLIENT_IP' ] ) )
    $ipaddress = $_SERVER[ 'HTTP_CLIENT_IP' ];
else if ( isset( $_SERVER[ 'HTTP_X_FORWARDED_FOR' ] ) )
    $ipaddress = $_SERVER[ 'HTTP_X_FORWARDED_FOR' ];
else if ( isset( $_SERVER[ 'HTTP_X_FORWARDED' ] ) )
    $ipaddress = $_SERVER[ 'HTTP_X_FORWARDED' ];
else if ( isset( $_SERVER[ 'HTTP_FORWARDED_FOR' ] ) )
    $ipaddress = $_SERVER[ 'HTTP_FORWARDED_FOR' ];
else if ( isset( $_SERVER[ 'HTTP_FORWARDED' ] ) )
    $ipaddress = $_SERVER[ 'HTTP_FORWARDED' ];
else if ( isset( $_SERVER[ 'REMOTE_ADDR' ] ) )
    $ipaddress = $_SERVER[ 'REMOTE_ADDR' ];
else
    $ipaddress = 'UNKNOWN';

return $ipaddress;
 }

Works perfectly. It records the user’s IP in a mysql table.

I decided to do a test, giving an echo on the PHP page, as shown below, so it display the user’s IP.

 echo get_client_ip();

When I use a device with wi-fi access, it displays the IP normally. The problem is that, when accessing this same PHP page through a mobile using 'mobile data', instead of displaying the IP of the same, it displays other information, as shown below and not your real IP:

 2804:d45:b18:94bc::1

The curious thing is that it saves the IP normally (and not that information as shown above), only when I ask to give echo above, it displays this way. Could you explain to me why?

  • There is no way to capture the real IP of the machine, if you test on any IP tracking site, will have captured the server IP, not the machine, I used this same function thinking q was right

  • 1

    But 2804:d45:b18:94bc::1 is an Ipv6, which IP it saves in Mysql?

  • Hm, he was saving in Ipv4

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.