php - Getting IPv6 support with php5-geoip and Maxmind database

983

I have geoip setup by following these identical steps (http://php.net/manual/en/geoip.setup.php):

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

sudo apt-get install php5-geoip

This works brilliantly and has for several years now with IPv4 addresses. I did not, however realize how broken it was with incoming IPv6 devices. For example, it returnsNotice: geoip_country_code_by_name(): Host 26xx:8:xx00:cf20:caxx:ff:fexx:35b5 not found

I have been through quite a few things attempting to fudge it into working, including:

  • testing for v6 address and usinggeoip_open("/usr/share/GeoIP/GeoIPv6.dat", GEOIP_STANDARD);

Apparently geoip_open is not an available function ala php5-geoip

  • I have tried usinggeoip_country_code_by_name_v6($ip)

This is another function that is not available even thoughgeoip_country_code_by_name($ip) works great.

No avail so far.

Is there any way I can easily, within my PHP code select which database I want to use so that I can simply test whether it is an IPv4 or IPv6 address and load the appropriate database?

662

Answer

Solution:

In order to support IPv6->Country code easily and without unnecessary files based on the integration above:

Grab a copy of the latest legacy IPv6 data (I'm assuming you already have IPv4 binary):

wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz

Decompress and move it to a dir accessible to your web server:

gunzip GeoIPv6.dat
mv GeoIPv6.dat /etc/usr/share/GeoIP/GeoIPv6.dat

Grab a copy of geoip.inc from the Maxmind git dir (https://github.com/maxmind/geoip-api-php/blob/master/src/geoip.inc) and save it somewhere you can access wherever you'll need to run geoip.

If you have php5-geoip installed as I did, remove it withsudo apt-get remove php5-geoip; purge as necessary.

With the above done you can now test incoming IP address for v4 or v6 and get appropriate results.

Example:

<?php
include_once('geoip.inc');

//set an IPv6 address for testing
$ip='2601:8:be00:cf20:ca60:ff:fe09:35b5';

/*
test if $ip is v4 or v6 and assign appropriate .dat file in $gi
run appropriate function geoip_country_code_by_addr() vs geoip_country_code_by_addr_v6()   
*/
if((strpos($ip, ":") === false)) {
    //ipv4
    $gi = geoip_open("/usr/share/GeoIP/GeoIP1.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, $ip);
}
else {
    //ipv6
    $gi = geoip_open("/usr/share/GeoIP/GeoIPv6.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr_v6($gi, $ip);
}
echo $ip . "<br>" . $country;

This is specifically for Country, but can easily be replicated for City data.

People are also looking for solutions to the problem: php - I am publish(emiting) through redis on node from laravel but all the process works in queue i want to run simultaneously

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.