さくらのレンタルサーバにGeoIPを入れる
IPアドレスから所在地が分かるという「GeoIP」。下記の記事を参考に、さくらのサーバでもインストールできるか試してみた。例によってbashを想定。
GeoIPライブラリ
$wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
$tar zxvf GeoIP.tar.gz
$cd GeoIP.tar.gz
$./configure --prefix=$HOME/local
$make
$make install
GeoIPデータ
参考ページにあるURLがそのまま使えなかったので、適宜変更。
$wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz $gunzip GeoIP.dat.gz $cp GeoIP.dat.gz ~/local/share/GeoIP$wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
$gunzip GeoLiteCity.dat.gz
$cp GeoLiteCity.dat.gz ~/local/share/GeoIP/GeoIPCity.dat
Peclモジュール
一度ダウンロードしてコンパイル。余計なconfigureオプションがあるかも。
$export PHP_AUTOCONF=autoconf259 $export PHP_AUTOHEADER=autoheader259.$pecl download geoip
$tar zxvf geoip-1.0.7
$cd geoip-1.0.7
$phpize
$./configure --prefix=$HOME/local --exec-prefix=$HOME/local --with-php-config=$HOME/local/bin/php-config --with-geoip=$HOME/local
$make
$make install
設定
php.iniにモジュールを設定。
extension_dir="/home/xxxxxx/local/lib/php/extensions/no-debug-non-zts-20060613/" extension="geoip.so"
テスト
var_dump(geoip_record_by_name('whitehouse.gov'));
array 'continent_code' => string 'AS' (length=2) 'country_code' => string 'SG' (length=2) 'country_code3' => string 'SGP' (length=3) 'country_name' => string 'Singapore' (length=9) 'region' => string '' (length=0) 'city' => string '' (length=0) 'postal_code' => string '' (length=0) 'latitude' => float 1.366700053215 'longitude' => float 103.80000305176 'dma_code' => int 0 'area_code' => int 0
シンガポール? MaxMind社のOnline Demoと結果が違うが、エラーログは吐いてない...。
var_dump(geoip_record_by_name($_SERVER['REMOTE_ADDR']));
array 'continent_code' => string 'AS' (length=2) 'country_code' => string 'JP' (length=2) 'country_code3' => string 'JPN' (length=3) 'country_name' => string 'Japan' (length=5) 'region' => string '40' (length=2) 'city' => string 'Tokyo' (length=5) 'postal_code' => string '' (length=0) 'latitude' => float 35.685001373291 'longitude' => float 139.75140380859 'dma_code' => int 0 'area_code' => int 0
とりあえず都道府県まで。緯度経度は皇居と出た...。