我是否错过了任何使此地理重定向代码正常工作的内容?

时间:2018-09-14 05:56:17

标签: php

我有一个登录页面www.A.com。我希望它重定向到第1级县的www.A.com/x,并重定向到所有其他国家的www.A.com/y。所有网站均已建立并且运行良好。我将geo.php放在了根文件夹中,将所有流量重定向到www.A.com/geo.php 尽管访问者来自1级国家/地区,但重定向到www.A.com/y却不能正常访问www.A.com/x

代码:

<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if (in_array($countrycode, array('us','gb','at','de','ca','ch','fr','it','nl','au','nz','sg','ch')))
    header( 'Location: http://www.A.com/x' ) ;
else 
    header( 'Location: http://www.B.com/y' ) ;
?>

请检查是否正确。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

请把$ countrycode转换为小写,一切正常。

<?php
function getUserIP()
{
    // Get real visitor IP behind CloudFlare network
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
        $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
        $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
    }
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}
$user_ip = getUserIP();
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$user_ip));
$countrycode= $a['geoplugin_countryCode'];
if (in_array(strtolower($countrycode), array('us','gb','at','de','ca','ch','fr','it','nl','au','nz','sg','ch')))
    header( 'Location: http://www.A.com/x' ) ;
else
   header( 'Location: http://www.B.com/y' ) ;
?>