user4951
user4951

Reputation: 33050

How to use geoip data for IPv6?

This is the code I used

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$buffer= geoip_country_code_by_addr($gi, $ip); 
geoip_close($gi);
return $buffer;

for IPv4.

How should I modify the code to support BOTH IPv6 and IP4?

Upvotes: 1

Views: 8702

Answers (1)

Greg Oschwald
Greg Oschwald

Reputation: 1735

In order to get this working, you will need to download the IPv6 database from MaxMind. You can find this at:

http://dev.maxmind.com/geoip/legacy/geolite/

After pointing geoip_open to the IPv6 file, you should be able to look up IPv6 addresses.

For example:

$database = (strpos($ip, ":") === false) ? "GeoIP.dat" : "GeoLiteCityv6.dat";
$gi = geoip_open($database, GEOIP_STANDARD);

The rest would be the same as before.

That said, you might want to consider refactoring so that you aren't opening and closing the database with each call.

Upvotes: 4

Related Questions