2014-09-26 51 views
2

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

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

回答

3

你不能在同一个PHP脚本一个php.ini,你不需要因为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'); 
    } 
?> 
+0

谢谢艾萨克。我使用的是共享服务器,因此我不知道“include/geoip.inc”的路径,我无法安装任何扩展程序或插件或更改服务器配置。我问提供者,但他们建议我改变路径/home/fuiba/php.ini,因为他们不能给我路径geoip.inc。 – 2014-09-26 15:11:06

+1

@Fuiba喜欢在我的文章中指定,你可以下载它在这个地址 https://github.com/maxmind/geoip-api-php/blob/master/src/geoip.inc 然后放在'包括/'文件夹或任何你想要的只是不要忘记把正确的道路,然后在'包括' – Isaac 2014-09-26 15:13:03

+0

非常感谢@Isaac – 2014-09-26 15:19:08