ipinfo.io Geolocation Use

Hi guys. Wondering if anyone out there has ever used ipinfo.io I'm trying to use their services to redirect users based on their ip location. The last code on the bottom is what I have but I keep getting redirected to Google.  This what they have as their usage examples:

Usage examples from their site:

A HTTPS JSONP request:
$.get("https://ipinfo.io?token=4#########", function (response) {
}, "jsonp");

Open in new window

From the command line:

$ curl ipinfo.io/
  "ip": "",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "phone": 650

Open in new window

This what I have:

function ip_details($ip) {
    $json = file_get_contents("http://ipinfo.io/?token=4#########/{$ip}");
    $details = json_decode($json);
    return $details;

$details = ip_details($_SERVER['REMOTE_ADDR']);
$yourcountry = $details->region;
if($yourcountry == "New York")
	header ('location: https://newyork.mydomain.com/home.php');
elseif($yourcountry == "New Jersey")
	header ('location: https://newjersey.mydomain.com/home.php');
	header ('location: https://google.com');

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$details = ip_details($_SERVER['REMOTE_ADDR']);
var_dump($details); /* what do you get here?  */
$yourcountry = $details->region;
Genesis5150Author Commented:
Ok what I'm getting is my server's location. Not the user's location. For region I got Utah and then substituted with ip instead ad got back my server's ip address. I think it has to do something to do with this $_SERVER['REMOTE_ADDR']
Save this as test.php
print_r( $_SERVER);

Open in new window

Now see which property has the client machine's IP.  It could be on REMOTE_ADDR or HTTP_X_FORWARDED_FOR.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Genesis5150Author Commented:
It shows:
[REMOTE_ADDR] => 74.xxx.xxx.xx
>> [REMOTE_ADDR] => 74.xxx.xxx.xx
OK, but is that your server's IP? Was there any other IP listed in any of the other properties other than your server's IP?  Perhaps one of these:
Genesis5150Author Commented:
[REMOTE_ADDR] => 74.xxx.xxx.xx "My IP Address"
[SERVER_ADDR] => 162.xxx.xxx.xx "My Server's IP Address"
Genesis5150Author Commented:
Those are the only 2 ip address
I'm guessing that https://www.whatismyip.com/ reports the same IP as REMOTE_ADDR, correct?

It is possible that the db info is wrong.  See if ipinfodb.com gives you different results:

Actually, this could explain what you are seeing:
Genesis5150Author Commented:
Figured the issue it was the position of {$ip} in file_get_contents("http://ipinfo.io/{$ip}?token=4xxxxxxxxxx");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Genesis5150Author Commented:
Figured the issue it was the position of {$ip} in file_get_contents("http://ipinfo.io/{$ip}?token=4xxxxxxxxxx");
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.