2

I know may be this questions asked here 1000 times, but i need a specific answer of my question here, I know my code is right but there is something else happening here which i don't know, so don,t duplicate my question please. Thanks :) I am using a code to get the IP address. Here is my code:

<?php
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
    $ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR'&amp;quot;);
else if(getenv('HTTP_X_FORWARDED'))
    $ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
    $ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
echo $ipaddress; // final ip will be here
?>

and the output is :

    ::1

I don't know why this giving me this number and not real IP, please help soon. Remember i am working on LOCALHOST

Devilism
  • 147
  • 2
  • 5
  • 20
  • possible duplicate of [How to get the client IP address in PHP?](http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php) – nomistic Jun 27 '15 at 17:42
  • that question doesn't answers my question, please have a look at my output – Devilism Jun 27 '15 at 17:45

1 Answers1

1

This output is the address of the server. It is an IPv6 address (equivalent of 127.0.0.1 in IPv4, which means localhost).

You are getting this because you are probably running this in localhost.

Rameleu
  • 105
  • 10
  • so how can i get the real ip address of user in localhost ? – Devilism Jun 27 '15 at 17:47
  • 1
    If you are in localhost, you can't have another address. If you run this on a distant server, then the value should change. What the server is doing is getting the IP of the person who request this page, and as the server runs in your private network, it doesn't search further. – Rameleu Jun 27 '15 at 17:49
  • so at least it should give me my ip address, which it doesn't ? – Devilism Jun 27 '15 at 17:49
  • 1
    It actually does. Your IP address (in your network) is 127.0.0.1 (or ::1 in IPv6) – Rameleu Jun 27 '15 at 17:53