Using MaxMind to locate visitors by IP address _ PHP Tutorial

Source: Internet
Author: User
Tags geoip geoip database maxmind
Use MaxMind to locate visitors based on IP addresses. Sometimes you need to know the country in which your site visitors come from-for example, if you are planning to launch an advertising program for a geographical region. This document introduces this method. Sometimes you need to know the country in which your site visitors come from-for example, if you are planning to launch an advertising program for a geographical region. This document introduces this method.

Sometimes you need to know the country in which your site visitors come from-for example, if you are planning to launch an advertising program for a geographical region. This is where a tool like MaxMind's GeoIP can show its strength-it allows you to easily obtain the exact geographic location information from the visitor's IP address.

MaxMind provides commercial databases and free databases. The former is more accurate and accurate to the user's city information level, while the latter can only determine the country and region. In this article, we will demonstrate how to use the free version. If you need more details such as remote customer city and country information, you need to buy more detailed databases 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 the Web server. Then you need to select the language API used by the database file. To simplify the entire process, we will use a pure PHP version to avoid additional configuration or Apache components. Please remember to read the terms of the software license before installing the software on the Web site: http://www.maxmind.com/download/geoip/database/license.txtto ensure that you agree to the terms.

Code List

// 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". handle ($ handle, "62.149.130.132"). "(country code". handle ($ handle, "62.149.130.132 ").")";

// Close database handler
// Www.knowsky.com

Geoip_close ($ handle );

// 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 method to use the module (geoip. inc) to access the GeoIP free national information database (GeoIP. dat. For example, if PHP include is in the same directory as the PHP file itself as the information database file of the National House. 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 use the handle returned by calling geoip_open () to 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 are also used (). 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.

What we do is that simple.

Bytes. This document introduces this method. Sometimes you need...

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.