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) {
    console.log(response.ip);
}, "jsonp");

Open in new window


From the command line:

$ curl ipinfo.io/8.8.8.8?token=4###########
{
  "ip": "8.8.8.8",
  "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:

<?php
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');
else
	header ('location: https://google.com');
?>

Open in new window

Genesis5150Asked:
Who is Participating?

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

x
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.

hieloCommented:
...
$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']
hieloCommented:
Save this as test.php
<?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
hieloCommented:
>> [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:
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
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
hieloCommented:
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:
https://css-tricks.com/snippets/php/detect-location-by-ip/

Actually, this could explain what you are seeing:
http://forums.xfinity.com/t5/Xfinity-com-Website/ISP-giving-wrong-geolocation-to-websites-for-the-last-couple/td-p/2458898
Genesis5150Author Commented:
Figured the issue it was the position of {$ip} in file_get_contents("http://ipinfo.io/{$ip}?token=4xxxxxxxxxx");

Thanks

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
PHP

From novice to tech pro — start learning today.