Differentiate users in different countries by IP Address

Source: Internet
Author: User
Tags geoip
Document directory
  • Distinguish countries by IP address in PHP
  • Differentiate countries by IP address in WordPress
  • Summary
Distinguish countries by IP address in PHP

How can I use PHP to distinguish countries and regions through IP addresses? Maxmind.com provides a set of GeoIP solutions. It takes only a few steps to determine the visitor's country through IP address in PHP.

1. Download database and PHP library files

  • Download GeoID.dat.gz and decompress it to the GeoIP. dat file.
  • Download geoip. inc.

2. Use PHP code to obtain country information
The following is a Demo code to show you how to obtain the Country Code and country name.

<? Php // introduce the PHP Library File include ("geoip. inc "); // open the local database and save the data in the GeoIP file. $ geoData = geoip_open ('geoip. dat ', GEOIP_STANDARD); // obtain the country IP address $ countryCode = geoip_country_code_by_addr ($ geoData, $ _ SERVER ['remote _ ADDR']); // obtain the country name $ countryName = geoip_country_name_by_addr ($ geoData, $ _ SERVER ['remote _ ADDR ']); // close the local database geoip_close ($ geoData);?>
Differentiate countries by IP address in WordPress

WordPress must be Okay since it is Okay to use PHP. Let's see how I use it.

1. Place database files
Decompress GeoIP. dat to the WordPress root directory (you can find the wp-config.php or wp-config-sample.php file in this directory)

2. Compile the call interface
Create a folder include in the topic directory, place geoip. inc in the new folder, and create the file geoip. php In the folder as follows.

<?php include('geoip.inc'); global $countryCode; $geoData = geoip_open('GeoIP.dat', GEOIP_STANDARD);$countryCode = geoip_country_code_by_addr($geoData, $_SERVER['REMOTE_ADDR']);geoip_close($geoData); ?>

The country code is used as the identification basis. The country code is a global variable to avoid repeated access to GeoIP. dat to obtain information and reduce program overhead.

2. Call the interface to obtain the Country Code
3. Open the header. php file and add the following code to the top of the file.

<?php include('include/geoip.php'); ?>

4. Use Country Code
Call the code in the topic, for example.

<? Php global $ countryCode; if ($ countryCode = 'cn') {// code executed in mainland China} else if ($ countryCode = 'us ') {// code executed in the United States} else {// code executed outside mainland China and the United States}?>
Summary

It is very accurate to identify visitor sources through IP addresses. Currently, some foreign trade websites use this method to display area scores to users. For example, users in the United States can see products and logistics information available in the United States by default. however, it is not accurate. For example, if someone goes through the wall all the year round, he may never see the information of his country. as to whether differentiated processing is required, the website is mainly considered.

Someone may ask, what is the performance of adding such an item? Will it require powerful servers? I tested it. Normal servers have almost no impact on page loading performance. You can check the speed of this blog. If you are not at ease, test it yourself.

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.