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


I have geoip setup by following these identical steps (

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?




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):


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 from the Maxmind git dir ( 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.



//set an IPv6 address for testing

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)) {
    $gi = geoip_open("/usr/share/GeoIP/GeoIP1.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, $ip);
else {
    $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


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.