Sometimes you need to know the country from which your site visitors come from-for example, if you are planning to perform the advertising program for the geographical region. This document introduces this method first. Sometimes you need to know which country your site visitors come from-for example, false
Sometimes you need to know the country from which your site visitors come from-for example, if you are planning to perform the advertising program for the geographical region. This document introduces this method first.
Sometimes you need to know the country from which your site visitors come from-for example, if you are planning to perform the advertising program for the geographical region. This is where a tool like MaxMind's GeoIP can show its skills-it allows you to easily obtain the geographic status information of visitors from their IP addresses.
MaxMind provides trade databases and free databases. The former is more accurate, and the accuracy can reach the information level of the city where the application is located, while the latter can only determine the country and region. In this article, we will demonstrate the free version of the application method. If you need more specific information, such as remote customer city and ***, you need to purchase more specific databases from MaxMind: http://www.maxmind.com.
Start
To apply this software, you must first download the GeoIP free *** File: http://www.maxmind.com/app/geoip_country and store it in a directory on the Web server. Then you need to select the language API used by the database file. To simplify the entire process, we will apply a pure PHP version to avoid additional configuration or Apache components. Please remember to view the software license terms before installing the software on the Web site: http://www.maxmind.com/download/geoip/database/license.txtto acknowledge your approval of these terms.
Code List
// Include functions
Include ('geoip. Inc ');
// Read GeoIP database
= Geoip_open ('geoip. dat ', GEOIP_STANDARD );
// Map IP to country
Echo 'IP address 62.149.130.132 located in '. geoip_country_name_by_addr (, '62. 149.130.132'). '(country code'. Reset (, '62. 149.130.132 ').')';
// Close database handler
// Www.knowsky.com
Geoip_close ();
// Print compulsory license notice
Echo'
-- This product has des GeoIP data created by MaxMind, available from http://maxmind.com /--';
?>
The code in List A shows the basic methods for the application module (geoip. inc) to visit the GeoIP free *** database (GeoIP. dat. The example assumes that PHP include and the information database files of the national family are in the same directory as the PHP file itself. If the example is different from your installation, you need to change the path as needed.
The sample code is quite clear. after the GeoIP PHP function library is introduced, the first step is to use the geoip_open () function to open the GeoIP database file. This function receives two parameters: database file path and database type.
We then apply the handle returned by calling geoip_open () and obtain the country code with two letters and an intuitive country name based on the given IP address. The geoip_country_code_by_addr () and geoip_country_code_by_name () functions must be distinguished (). Both receive two parameters: the handle returned by geoip_open () and the IP address to be resolved.
Once the required information is obtained, we call geoip_close () to close the database file.