Transferred from: http://www.21andy.com/blog/20100405/1799.html
There are 2 standard methods of using GeoIP
The first method:
Download the GeoIP data file first
Wget-c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
Gunzip GeoIP.dat.gz
Then download GeoIP PHP file, save as Geoip.php
Http://geolite.maxmind.com/download/geoip/api/php/geoip.inc
All right, just use it.
Timing starts
function Utime () {
$time = Explode ("", Microtime ());
$usec = (double) $time [0];
$sec = (double) $time [1];
return $usec + $sec;
}
$startTimes = Utime ();
Include the PHP script
Wget-c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
Gunzip GeoIP.dat.gz
Include ("geoip.php");
Open the GeoIP database
$gi = Geoip_open ("GeoIP.dat", Geoip_standard);
Get Country Code
$country _code = geoip_country_code_by_addr ($gi, $_server [' remote_addr ']);
echo "Your Country code is:$country _code
" ;
Get Country Name
$country _name = geoip_country_name_by_addr ($gi, $_server [' remote_addr ']);
echo "Your Country name is:$country _name
" ;
Close the database
Geoip_close ($GI);
Run End time
$endTimes = Utime ();
$runTimes = sprintf ('%0.4f ', ($endTimes-$startTimes));
echo "Processed in". $runTimes. "Second.";
?>
This method uses a binary file as a database, which is fast and does not require a database. I tried the execution time is 0.001, complete 1000 times per second is not a problem.
The second method:
Installing GeoIP into PHP extensions
Yum Install GeoIP Geoip-data geoip-devel
Download GeoIP Database
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Gzip-d GeoLiteCity.dat.gz
MV Geolitecity.dat/var/lib/geoip/geoipcity.dat
Download GeoIP PECL Extension
Download Address Http://pecl.php.net/package/geoip
Wget-c http://pecl.php.net/get/geoip-1.0.7.tgz
TAR-ZXVF geoip-1.0.7.tgz
Installing the GeoIP PECL extension
CD geoip-1.0.7
/usr/bin/phpize
./configure--with-php-config=/usr/bin/php-config--with-geoip
Make
Make install
Add in the php.ini.
Extension=geoip.so
Then restart PHP.
Service httpd Restart
Now, you can use the GeoIP partial function in the PHP manual.
http://cn.php.net/manual/en/book.geoip.php