如何在共享服务器上配置GeoIP?

时间:2014-09-26 14:56:26

标签: php geoip

我想配置 GeoIP ,根据共享服务器中的国家/地区IP地址将域重定向到子域。我创建了一个自定义php.ini来导入 geoip.so 然后在我的index.php中添加了此代码:

<?php
    require_once('/home/fuiba/php.ini');
    $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

    geoip_close($gi);
    $my_countries = 'fr';
    if (strtolower($country) == $my_countries) {
        header('Location: fr.fuiba.org');
    }
    $my_countriessss = 'us';
    if (strtolower($country) == $my_countriessss) {
        header('Location: en.fuiba.org');
    }
?>

在浏览器中我收到此错误:

extension=geoip.so
Fatal error: Call to undefined function geoip_open() in /home/fuiba/public_html/index.php on line 3

GeoIP安装在服务器中。我在info.php上检查了它: geoip版本1.0.8

enter image description here

1 个答案:

答案 0 :(得分:3)

您不能将php.ini包含在php脚本中,并且您不需要自phpinfo()返回已安装的脚本。

为了使GeoLite工作,您需要做的是首先包含geoip.inc文件include("include/geoip.inc");

如果你还没有它,你可以在这里找到它: https://github.com/maxmind/geoip-api-php/blob/master/src/geoip.inc

<?php
   include("include/geoip.inc");
   $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

    geoip_close($gi);
    $my_countries = 'fr';
    if (strtolower($country) == $my_countries) {
        header('Location: fr.fuiba.org');
    }
    $my_countriessss = 'us';
    if (strtolower($country) == $my_countriessss) {
        header('Location: en.fuiba.org');
    }
?>
相关问题