Use Maxmind to position visitors based on IP address _php tutorial

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

  • Related Article

    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.