2

When I check ip from ipconfig on local host. I am getting ip like this 100.78.80.xxx (US), when I check IP on some website like ip2location/ip2nation, I am getting IP like this 115.245.20.xxx (IN). I want to know why?

I want to get the user IP address to get the country name. I am using $_SERVER['REMOTE_ADDR'] to get the user IP address and will get the country name.

Does it get the correct IP of user machine to get the correct country code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • possible duplicate http://stackoverflow.com/q/12553160/4323504 – Luthando Ntsekwa Jun 05 '15 at 05:24
  • no its not a duplicate. I checked that question. i am not looking for any code. I just asked the question to clear my doubt. – Roxx Jun 05 '15 at 05:27
  • 1
    possible duplicate of [What is the most accurate way to retrieve a user's correct IP address in PHP?](http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – Yeldar Kurmangaliyev Jun 05 '15 at 05:51

2 Answers2

4

It get the IP address of user's gateway or router, user's Ip address may varies you can't get it when user uses the router, but this IP is sufficient to get the county and it will give you the correct country.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sarvagna Mehta
  • 334
  • 3
  • 16
3

Luthando Loot is right. The answer is present here: Getting visitors country from their IP

However, it is located in answer but not in question.

if (isset($_SERVER['HTTP_CLIENT_IP']))
{
    $real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
    $real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
    $real_ip_adress = $_SERVER['REMOTE_ADDR'];
}

You should check this values in such order:

HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
REMOTE_ADDR

It doesn't mean that you will precisely get his IP address. It can be easily hidden by anonymizer, proxy, ISP etc. However, it is much more efficient than loooking at REMOTE_ADDR only.

There are some other values in SERVER array. Actually, this question is a duplicate to this:
What is the most accurate way to retrieve a user's correct IP address in PHP?

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101