Use MaxMind to locate visitors based on IP addresses

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

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.