Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP中的国家/地区重定向用户_Php_Web - Fatal编程技术网

通过PHP中的国家/地区重定向用户

通过PHP中的国家/地区重定向用户,php,web,Php,Web,我试图用PHP将用户重定向到一个关于他们国家的适当页面。例如,美国-mysite.com,澳大利亚-mysite.com.au。我已经通过此功能获得了IP地址,并希望使用此网站获取他们的国家:http://api.hostip.info/country.php?ip= if (getenv(HTTP_X_FORWARDED_FOR)) { $ip_address = getenv(HTTP_X_FORWARDED_FOR); echo $ip_address; } else

我试图用PHP将用户重定向到一个关于他们国家的适当页面。例如,美国-mysite.com,澳大利亚-mysite.com.au。我已经通过此功能获得了IP地址,并希望使用此网站获取他们的国家:
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);
我建议使用,但你能具体告诉我们问题是什么吗?你所做的只是告诉我们你想做什么,但你从来没有告诉我们你当前的代码是如何出现故障的

注意:Netimpact已关闭。他们已经将用户迁移到了一个更广泛的API产品:IP2GEO、IP2Company、Domain2IP

但是,如果您只想获取该URL的内容,那就很容易了。只需在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');
?>


如果不调用API,您可能需要研究如何使用感谢。我已经设法让它工作,但它给出了错误:重定向页面时无法修改标题信息:if($Country==“US”){header('Location:);}否则如果($Country=='AU'){header('Location:);}您必须在标题之前有输出。如果您不想更改任何现有代码,只需缓冲输出。把这个放在页面顶部,然后再做其他事情:ob_start();