Tutorial on using PHP GeoIP

Source: Internet
Author: User
Tags config geoip geoip database php file
GeoIP Introduction:
What is GEPIP?
The so-called GeoIP, is through the visitor's IP, positioning his latitude and longitude, country/region, provinces and cities, and even streets and other location information. The technology is not a problem, the key is to have a precise database. With accurate data sources on the enjoy high earn a little money, but to carry forward the spirit of cooperation, collective contribution to all people to enjoy is our pursuit.
How to use GeoIP?
First we need data information, so first get a free database: GeoIP.dat.gz, then extract: GeoIP.dat, then the data file on-demand operation, this example uses PHP.
the use of GeoIP + PHP
Method One:
Download the GeoIP PHP file geoip.inc. Package download
Copy CodeThe code is as follows:
Include ("geoip.inc.php");
Open Data File
$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 File
Geoip_close ($GI);

Note: If you test locally because $_server[' remote_addr ' and $_server[' remote_addr ' may be 127.0.0.1, the output is empty. You can bring your own IP test

Method Two:
Install the GeoIP into a PHP extension
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 the 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

Install the GeoIP PECL extension
CD geoip-1.0.7
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config--with-geoip
Make
Make install

Add in the php.ini.
Extension=geoip.so
Then you can restart PHP.
Now, you can use the GeoIP partial function in the PHP manual.

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.