Reputation: 647
I'm trying to redirect a user to an appropriate page with respect to their country in PHP. Eg. US - mysite.com, Australia - mysite.com.au. I already have the IP address via this function and want to use this site to get their country: http://api.hostip.info/country.php?ip=
.
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip_address = getenv(HTTP_X_FORWARDED_FOR);
echo $ip_address;
} else {
$ip_address = getenv(REMOTE_ADDR);
echo $ip_address;
}
ip_address_to_number($ip_address);
Upvotes: 0
Views: 669
Reputation: 2259
I would suggest using NetImpact's API, but can you tell us specifically what the problem is? All you did was tell us what you're trying to do but you never told us how your current code is malfunctioning.
NOTE: Netimpact has shut down. They have migrated users to KickFire, which has a broader API offering: IP2GEO, IP2Company, Domain2IP.
However, if all you want to do is get the content of that URL it's really easy. Just put this in your PHP page:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'http://api.hostip.info/country.php?ip=' . $ip;
$country = file_get_contents($url);
// Do something with the country, I am forwarding to a subdomain here but you can
// do whatever you like.
header('Location: http://' . $country. '.mysite.com');
?>
Upvotes: 2