【问题标题】:GeoRedirection script not working properly地理重定向脚本无法正常工作
【发布时间】:2013-11-16 02:31:41
【问题描述】:


我正在开发一个特定于国家/地区的网站,我认为在我的 php 服务器上安装了地理重定向。基本方法是将传入的 IP 地址记录在 $_SERVER['REMOTE_ADDR'] 中,然后使用地理定位数据库将其转换为国家信息。我确实使用了 Maxmind 地理定位服务。 我用一些谷歌能力和实验能力得出的最终脚本如下

index.php

<?php

getCountry($_SERVER['REMOTE_ADDR']);
$ip=$_SERVER['REMOTE_ADDR'];

function getCountry($ipAddress)
        {

                // get the country of the IP from the MAXMIND
                $country="";

                // include functions
                include("geoip.inc.php");

                // read GeoIP database
                $handle = geoip_open("GeoIP.dat", GEOIP_STANDARD);


                // map IP to country
                $country = geoip_country_name_by_addr($handle, $ipAddress);

                // close database handler
                geoip_close($handle);

                if ($country==""|| empty($country))
                {

                        $country="Unknown";
                }


                return $country;


        }

$country_code = geoip_country_code_by_addr($gi, "$ip");

// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code)

        {
            case "US": header("Location: http://site1.com
    "); break;
            case "CA": header("Location: http://site2.com
    "); break;
            case "GB": header("Location: http://site3.com
    "); break;
            default: header("Location: http://site.com
    ");
    }


?>

我从https://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc下载了geoip.inc

GeoIP.dat来自http://dev.maxmind.com/geoip/legacy/geolite/

但是 某处出了问题。代码不执行,每次都有一些错误。尝试了很多愚蠢的错误排列,但没有任何效果。调用 URL 会在 URL 上显示文件 geoip.inc 的代码。

试图在 SO 上找到任何类似的问题,但找不到。详细的帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: php api geolocation ip


    【解决方案1】:

    你需要应用一个 php 扩展来确保 inc 被处理为 PHP

    添加函数并调用getCountry($_SERVER['REMOTE_ADDR']);

    请注意,有些人可能没有设置此服务器变量或者它是准确的 - 代理的 cos 或其他东西 在与脚本相同的文件夹中使用 geoip.inc.php 和 GeoIP.dat

    Edited : 修改你编辑的代码

    // 包含函数 包括(“geoip.inc.php”);

    $ip=$_SERVER['REMOTE_ADDR'];
    $country_code = getCountry($ip);
    
    switch($country_code)
    
            {
                case "US": header("Location: http://site1.com
        "); break;
                case "CA": header("Location: http://site2.com
        "); break;
                case "GB": header("Location: http://site3.com
        "); break;
                default: header("Location: http://site.com
        ");
        }
    
    
        function getCountry($ipAddress)
                {
    
                        // get the country of the IP from the MAXMIND
                        $country="";
    
    
    
                        // read GeoIP database
                        $handle = geoip_open("GeoIP.dat", GEOIP_STANDARD);
    
    
                        // map IP to country
                        $country = geoip_country_name_by_addr($handle, $ipAddress);
    
                        // close database handler
                        geoip_close($handle);
    
                        if ($country==""|| empty($country))
                        {
    
                                $country="Unknown";
                        }
    
    
                        return $country;
    
    
                }
    

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2015-08-09
      • 2014-07-01
      • 2015-12-21
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多