Using Maxmind to locate visitors based on their IP address

Source: Internet
Author: User
Tags geoip geoip database implement include php file maxmind
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.

Start

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

<?php

Include functions

Include ("Geoip.inc");

Read GeoIP Database

$handle = Geoip_open ("GeoIP.dat", Geoip_standard);

Map IP to Country

echo "IP address 62.149.130.132 located in". GEOIP_COUNTRY_NAME_BY_ADDR ($handle, "62.149.130.132"). "(Country code.) GEOIP_COUNTRY_CODE_BY_ADDR ($handle, "62.149.130.132"). ")";

Close Database Handler
www.knowsky.com

Geoip_close ($handle);

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.



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.