2012-12-30 31 views
0

我正在尝试将用户重定向到相应的国家/地区的PHP页面。例如。美国 - mysite.com,澳大利亚 - mysite.com.au。我已通过此功能获得IP地址,并希望使用此网站获取其国家/地区:http://api.hostip.info/country.php?ip=在PHP中通过国家重定向用户

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); 
+0

与其说你可能想看看API的使用到[GeoIP的(http://php.net/manual/en/book.geoip.php) – PhearOfRayne

回答

2

我会建议使用NetImpact's API,但是您能特别告诉我们问题是什么吗?你所做的只是告诉我们你想要做什么,但是你从未告诉我们你当前的代码是如何发生故障的。

注意:Netimpact已关闭。他们已将用户迁移到KickFire,其中有更广泛的API产品:IP2GEO,IP2Company,Domain2IP。

但是,如果您只想获取该网址的内容,那就很容易了。只要把这个在你的PHP页面:

<?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'); 
?> 
+0

谢谢。我已经成功地获取工作,但它给错误:无法修改重定向页面时标题信息:如果($国家==“US”) \t \t { \t \t \t头('位置:http:/ /mysite.com'); \t \t} \t \t \t \t否则,如果($国家== 'AU') \t \t { \t \t \t头( '位置:http://mysite.com.au'); \t \t} – liamp47

+0

您必须在标题之前有输出。如果你不想改变任何现有的代码,只需缓冲输出。把它放在页面顶部之前:ob_start(); – SISYN