GeoIP / IP Geolocation
I love it! If you are from a hosting company you may find this interssting
because not all using this and only offer the geoip.dat file via apache
eviroment variables. too bad. these are all free to use databases for the
php-geoip extension which is much more fun to work with.
Thanks to all-inkl.com to make it happen for me when i asked in that time 🙂
I can use them as PHP Apache module enviroment variables because the php-geo extension is not available but this is fine for me)
Source Informations:
http://dev.maxmind.com/geoip/legacy/geolite/
IP Geolocation
The GeoLite databases are our free IP geolocation databases. They are updated
on the first Tuesday of each month. (CRONJOB!)
These databases are offered in the same binary and csv formats as our
subscription databases. Any code which can read the subscription databases
can also read the GeoLite databases.
http://dev.maxmind.com/geoip/legacy/install/city/
http://dev.maxmind.com/geoip/legacy/geolite
Attached a little script (for root) which does the download and installs the databases in /usr/share/GeoIP/
Then you are able to use all of them via php-geoip extension.
Cronjob:
# checks twice a month and loads it if they are newer 10 3 6 */2 * /path/to/geoIPupdates.sh.php
On error the script reports the errors otherwise it works silent. So mails from cron will only inform you on errors
#!/usr/bin/env php <?php /** * GeoIP / IP Geolocation * * I love it! If you are from a hosting company you may find this interssting * because not all using this and only offer the geoip.dat file via apache * eviroment variables. too bad. these are all free to use databases for the * php-geoip extension which is much more fun to work with. * Thanks to all-inkl.com to make it happen for me when i asked in that time :-) * * Source Informations: * http://dev.maxmind.com/geoip/legacy/geolite/ * * IP Geolocation * The GeoLite databases are our free IP geolocation databases. They are updated * * on the first Tuesday of each month. (CRONJOB!) * * These databases are offered in the same binary and csv formats as our * subscription databases. Any code which can read the subscription databases * can also read the GeoLite databases. * * http://dev.maxmind.com/geoip/legacy/install/city/ * http://dev.maxmind.com/geoip/legacy/geolite/ */ error_reporting(-1); $pathTarget = '/usr/share/GeoIP/'; $pathTmp = '/tmp/'; chdir( $pathTmp ); $downloads = array( 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz', 'http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz', 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz', 'http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz', 'http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz', ); function execute($cmd) { $lastLine = exec($cmd, $output, $execStatus); if ($output||$lastLine||$execStatus) { echo 'Status code: ' . $execStatus . PHP_EOL; echo 'cmd: ' . $cmd . PHP_EOL; echo $lastLine . print_r($output, true); return false; } return true; } $copyCmds = array(); foreach($downloads as $url) { $output = array(); $basename = basename($url); $filename = substr($basename, 0, -3); // --- download to tmp ----------------------------------------------------- $cmd = sprintf( 'wget -N --quiet "%1$s" --output-document="%2$s/%3$s"', $url, $pathTmp, $basename ); if (execute($cmd)===false) { $copyCmds[] = $cmd; } // --- de-compress --------------------------------------------------------- $cmd = 'gunzip ' . $basename; if (execute($cmd)===false) { $copyCmds[] = $cmd; } // --- install ------------------------------------------------------------- if ( is_writable($pathTarget) ) { // copy to target, special: => 'GeoIPCity.dat' if ($filename == 'GeoLiteCity.dat') { copy($filename, 'GeoIPCity.dat'); $cmd = 'mv -f "' . $pathTmp.'/'.$filename .'" ' . $pathTmp .'/GeoIPCity.dat ' . $pathTarget; } else { $cmd = 'mv -f "' . $pathTmp.'/'. $filename .'" ' . $pathTarget; } if (execute($cmd)===false) { $copyCmds[] = $cmd; } } else { $copyCmds[] = $cmd; } } if ($copyCmds) { echo 'list of cmds which fails:'.PHP_EOL; echo implode(PHP_EOL, $copyCmds); }
Usage:
If you have them as enviroment variables in your phpinfo() you will find some entrys like this in the $_SERVER array:
GEOIP_ADDR 192.168.0.1 GEOIP_CONTINENT_CODE EU GEOIP_COUNTRY_CODE DE GEOIP_COUNTRY_NAME Germany GEOIP_REGION 04 GEOIP_REGION_NAME Hamburg GEOIP_CITY Hamburg GEOIP_DMA_CODE 0 GEOIP_AREA_CODE 0 GEOIP_LATITUDE 53.49 GEOIP_LONGITUDE 10.28 GEOIP_POSTAL_CODE 20095
What to do with it? Simple: Because this WordPress/Blog gets attacked from countrys far away. I decided to block user registration for people out of germany. All can read. But mostly in here is german content so discussions may come from germans.
<?php if ($_SERVER['GEOIP_COUNTRY_CODE'] != 'DE') { exit('Sorry, registration only available from germany'); }
If you have them available e.g. as apache enviroment variables you could put the following to your htaccess file to only allow users from DE (like in this example):
SetEnvIf GEOIP_COUNTRY_CODE DE AllowCountry Deny from all Allow from env=AllowCountry
1 Pingback