3

I want to configure GeoIP to redirect domain to a subdomains according to country IP address in a shared server. I have created a custom php.ini to import geoip.so then in my index.php I added this code:

<?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');
    }
?>

In the browser I get this error:

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

The GeoIP is installed in the Server. I checked it on info.php: geoip version 1.0.8.

enter image description here

Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107

1 Answers1

4

You can't include a php.ini with a php script, and you don't need to since phpinfo() return that it's already installed.

What you need to do in order to make GeoLite work is to first include geoip.inc file include("include/geoip.inc");

Here is where you can find it if you don't already have it : 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');
    }
?>
Isaac
  • 983
  • 1
  • 7
  • 13
  • Thanks Isaac. I am using a shared server so I don't know the path for `include/geoip.inc` and I can't install any extension or plugin or to change the server configuration. I asked the provider but they suggested me to change path to /home/fuiba/php.ini because they ca not give me the path geoip.inc. – Mustapha Aoussar Sep 26 '14 at 15:11
  • 1
    @Fuiba Like a specified in my post you can download it at this adress https://github.com/maxmind/geoip-api-php/blob/master/src/geoip.inc Then put in the `include/` folder or whatever you want just don't forget to put the right path then in the `include` – Isaac Sep 26 '14 at 15:13
  • @ChrysUgwu `$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);` – Mustapha Aoussar Dec 28 '15 at 13:39