Sometimes you need to know which country your site visitor is from--if you're planning to implement an advertising plan for your geographic area. This method is described in this article.
Sometimes you need to know which country your site visitor is from--if you're planning to implement an advertising plan for your geographic area. This is where the tools like Maxmind's GeoIP--it allows you to easily get the exact location information from the visitor's IP address.
Maxmind provides a business database and a free database. The former is more accurate, the precision can reach the user's city information level, while the latter can only identify countries and regions. In this article, we'll show you how to use the free version. If you need more detailed information, such as a remote customer's city and country information, you will need to purchase a more detailed database from maxmind:http://www.maxmind.com.
To use this software, you must first download the GeoIP free Country information file: Http://www.maxmind.com/app/geoip_country and store it in a directory on your Web server. Then you need to select the language API used by the database file. To simplify the process, we will use a pure PHP version to avoid additional configuration or to set up the Apache component. Remember to read the SOFTWARE License Terms before installing the software to the Web site: http://www.maxmind.com/download/geoip/database/LICENSE.txt to ensure that you agree to these terms.
Code List A
Read GeoIP Database
$handle = Geoip_open ("GeoIP.dat", Geoip_standard);
Map IP to Country
echo "IP address 220.127.116.11 located in". GEOIP_COUNTRY_NAME_BY_ADDR ($handle, "18.104.22.168"). "(Country code.) GEOIP_COUNTRY_CODE_BY_ADDR ($handle, "22.214.171.124"). ")";
Close Database Handler
Print Compulsory license notice
Echo <p>--This product includes GeoIP the data created by Maxmind, available from http://maxmind.com/ -";
The code in List A shows the basic method of using modules (GEOIP.INC) to access the GEOIP free National Information Database (GEOIP.DAT). The example assumes that both the PHP include and the home Information database file are in the same directory as the PHP file itself. If the sample is different from your installation, you need to change the path as needed.
The example code is quite clear, and after the introduction of the GeoIP PHP Library, the first step is to use the Geoip_open () function to open the GeoIP database file. This function receives two parameters: the database file path and the database type.
We then use the handle returned by the call Geoip_open () to obtain the two-letter country code and the intuitive country name based on the given IP address. The function geoip_country_code_by_addr () and Geoip_country_code_by_name () are also used separately. Both receive two parameters: the handle returned by Geoip_open () and the IP address that needs to be resolved.
Once you have the required information, we close the database file by calling Geoip_close ().
What I did was so simple.