admin管理员组文章数量:1026989
How can I get the user's country and forward them to a URL based on it?
I am looking specifically at Australia, New Zealand, USA, UK and Other.
Preferably with PHP, but can use Javascript if necessary.
How can I get the user's country and forward them to a URL based on it?
I am looking specifically at Australia, New Zealand, USA, UK and Other.
Preferably with PHP, but can use Javascript if necessary.
Share Improve this question asked Nov 25, 2013 at 5:25 JaceGJaceG 1243 silver badges16 bronze badges3 Answers
Reset to default 4This may help you to get country then you can redirect accordingly.
<?PHP
function visitor_country()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$result = "Unknown";
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$result = $ip_data->geoplugin_countryName;
}
return $result;
}
echo visitor_country(); // Output Coutry name [Ex: United States]
?>
OR in Javascript.
<html>
<head>
<script language="JavaScript" src="http://www.geoplugin/javascript.gp" type="text/javascript"></script>
</head>
<body>
<script language="Javascript">
document.write("Wele to our visitors from "+geoplugin_city()+", "+geoplugin_countryName());
// window.location = "http://www.yoururl."; /* you can forward user to url using this line of code according to your conditions
</script>
</body>
</html>
Please check the below link.
How can I detect visitor's country and redirect he/she to an specific website?
May be it will help you.. :)
You can use PECL's geoip_country_by_name()
with the user's ip address to get their country:
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$country = geoip_country_name_by_name($ip);
if ($country) {
echo 'This host is located in: ' . $country;
//header("Location: " . $somelocation);
}
?>
How can I get the user's country and forward them to a URL based on it?
I am looking specifically at Australia, New Zealand, USA, UK and Other.
Preferably with PHP, but can use Javascript if necessary.
How can I get the user's country and forward them to a URL based on it?
I am looking specifically at Australia, New Zealand, USA, UK and Other.
Preferably with PHP, but can use Javascript if necessary.
Share Improve this question asked Nov 25, 2013 at 5:25 JaceGJaceG 1243 silver badges16 bronze badges3 Answers
Reset to default 4This may help you to get country then you can redirect accordingly.
<?PHP
function visitor_country()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$result = "Unknown";
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$result = $ip_data->geoplugin_countryName;
}
return $result;
}
echo visitor_country(); // Output Coutry name [Ex: United States]
?>
OR in Javascript.
<html>
<head>
<script language="JavaScript" src="http://www.geoplugin/javascript.gp" type="text/javascript"></script>
</head>
<body>
<script language="Javascript">
document.write("Wele to our visitors from "+geoplugin_city()+", "+geoplugin_countryName());
// window.location = "http://www.yoururl."; /* you can forward user to url using this line of code according to your conditions
</script>
</body>
</html>
Please check the below link.
How can I detect visitor's country and redirect he/she to an specific website?
May be it will help you.. :)
You can use PECL's geoip_country_by_name()
with the user's ip address to get their country:
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$country = geoip_country_name_by_name($ip);
if ($country) {
echo 'This host is located in: ' . $country;
//header("Location: " . $somelocation);
}
?>
本文标签: javascriptHow to get user39s country and forward to URLStack Overflow
版权声明:本文标题:javascript - How to get user's country and forward to URL? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745570993a2156736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论