Genesis5150
asked on
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:
From the command line:
This what I have:
Usage examples from their site:
A HTTPS JSONP request:
$.get("https://ipinfo.io?token=4#########", function (response) {
console.log(response.ip);
}, "jsonp");
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
}
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');
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Save this as test.php
Now see which property has the client machine's IP. It could be on REMOTE_ADDR or HTTP_X_FORWARDED_FOR.
<?php
print_r( $_SERVER);
Now see which property has the client machine's IP. It could be on REMOTE_ADDR or HTTP_X_FORWARDED_FOR.
ASKER
It shows:
[REMOTE_ADDR] => 74.xxx.xxx.xx
[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:
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
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
ASKER
[REMOTE_ADDR] => 74.xxx.xxx.xx "My IP Address"
[SERVER_ADDR] => 162.xxx.xxx.xx "My Server's IP Address"
[SERVER_ADDR] => 162.xxx.xxx.xx "My Server's IP Address"
ASKER
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:
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured the issue it was the position of {$ip} in file_get_contents("http://ipinfo.io/{$ip}?t oken=4xxxx xxxxxx");
ASKER