"Go" GeoIP + PHP full guide

Source: Internet
Author: User
Tags geoip geoip database maxmind
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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.