【问题标题】:php - redirect visitor based on ip address country codephp - 根据 IP 地址国家代码重定向访问者
【发布时间】:2015-01-16 04:04:09
【问题描述】:

我在这里尝试使用 Chandra 的答案:Getting visitors country from their IP

这是 Chandra 的函数:

<?php

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>

这是 Chandra 关于如何使用的示例之一:

<?php

echo ip_info("Visitor", "Country"); // India
echo ip_info("Visitor", "Country Code"); // IN
echo ip_info("Visitor", "State"); // Andhra Pradesh
echo ip_info("Visitor", "City"); // Proddatur
echo ip_info("Visitor", "Address"); // Proddatur, Andhra Pradesh, India

print_r(ip_info("Visitor", "Location")); // Array ( [city] => Proddatur [state] => Andhra Pradesh [country] => India [country_code] => IN [continent] => Asia [continent_code] => AS )

?>

我可以让示例正常工作,但我想存储该数据以供以后调用,并使用 if 语句将英国访问者发送到我的 .co.uk 域而不是 .com。这就是我目前所拥有的。我所做的所有努力都将任何人和每个人重定向到 targetURL;不仅仅是来自英国的人。我尝试了不同的语法变体,但我是一个相当低级的编码器。

<?php

$c = ip_info("Visitor", "Country Code");
if ($c = "GB") {header("Location: targetURL");}

?> 

我们将不胜感激任何帮助、建议、建议等。

【问题讨论】:

    标签: php redirect


    【解决方案1】:

    您使用了错误的运算符。你想要$c == "GB" $c = "GB" 它只是为变量$c 赋值,因此总是返回true。

    $c = ip_info("Visitor", "Country Code");
    
    if ($c == "GB") {
        header("Location: targetURL");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2015-03-01
      • 1970-01-01
      • 2012-04-07
      • 2012-04-22
      • 1970-01-01
      • 2013-01-13
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多