Sometimes you need to know which country your site visitors come from-for example, if you're planning to implement an ad plan for a geographic area. This method is described in this article.
Sometimes you need to know which country your site visitors come from-for example, if you're planning to implement an ad plan for a geographic area. This is where tools like Maxminds GeoIP are used-it allows you to easily get their exact geolocation information from the visitor's IP address.
Maxmind provides a business database and a free database. The former is more accurate, the accuracy can reach the user's city information level, while the latter can only determine the country and region. In this article, we'll show you how to use the free version. If you need more details, such as a remote customer's city and country information, you'll 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 the Web server. Then you need to select the language API used by the database file. To simplify the process, we will use a purely PHP version to avoid other additional configurations or to set up Apache components. Please 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 make sure you agree to these terms.
Code Listing A
Read GeoIP Database
$handle = Geoip_open ("GeoIP.dat", Geoip_standard);
Map IP to Country
echo "IP address 184.108.40.206 located in". GEOIP_COUNTRY_NAME_BY_ADDR ($handle, "220.127.116.11"). "(Country Code"). GEOIP_COUNTRY_CODE_BY_ADDR ($handle, "18.104.22.168"). ")";
Close Database Handler
Print Compulsory license notice
--This product includes GeoIP data created by MaxMind, available from http://maxmind.com/--";
The code in listing 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 National Home Information database files 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 straightforward, and after introducing the GeoIP PHP Library, the first step is to open the GeoIP database file using the Geoip_open () function. This function receives two parameters: a database file path and a database type.
We then use the handle returned by the call Geoip_open () to obtain a two-letter country code and an 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 respectively. Both receive two parameters: the handle returned by Geoip_open () and the IP address that needs to be resolved.
Once you get the information you need, we close the database file by calling Geoip_close ().
What you do is so simple.
http://www.bkjia.com/PHPjc/532309.html www.bkjia.com true http://www.bkjia.com/PHPjc/532309.html techarticle Sometimes you need to know which country your site visitors come from-for example, if you're planning to implement an ad plan for a geographic area. This method is described in this article. Sometimes you need to ...