Php 地理定向脚本工作不正常

Php 地理定向脚本工作不正常,php,api,geolocation,ip,Php,Api,Geolocation,Ip,我正在开发一个特定于国家/地区的网站,我想在我的php服务器上安装一个地理重定向。基本方法是在$\u SERVER['REMOTE\u ADDR']中记录传入的IP地址,然后使用地理位置数据库将其转换为国家信息。我确实使用了Maxmind地理定位服务。 最后的脚本,我通过一些谷歌的权力和实验的权力是如下 index.php 我从 和GeoIP.datfrom 但是 某处出了问题。代码没有执行,每次都有一些错误。我尝试了许多愚蠢错误的排列,但都不起作用。调用URL会在URL上显示文件geoip


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

index.php

我从

GeoIP.datfrom

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


试图找到任何类似的问题,但无法。如能提供详细帮助,将不胜感激。提前感谢。

您需要应用一个php扩展,以确保inc作为php处理

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

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

已编辑:修改已编辑的代码

//包含函数 包括(“geoip.inc.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
    ");
    }


?>
$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;


            }