What's the use of distinguishing guest countries?
Here are a few examples of my use of this feature.
1. Distinguish the function of the website
This blog has the function of translating articles, this is for the convenience of overseas visitors to read the article, but for the Chinese appear very superfluous. So I judge the country by IP, the Chinese mainland Area shielding translation function.
2. Differentiating Display ads
Mainland China, for example, sees the bottom of the sidebar as a blessing, while the rest of the country sees Google's ads. Hostucan is an advertiser of mine, has English website, also has Chinese website, so I can provide him to differentiate the display service, lest waste flow.
3. Shielding Cloth Point Service
Overseas have a lot of good service platform, in the Web site can collect data and share articles. Unfortunately, because of some reasons, they show in the domestic effect is not good, not only did not play the due effect, but also let the page load time to become longer. These spots can be shielded from mainland visitors.
Differentiate countries in PHP via IP
How to use PHP to differentiate between countries and regions through IP? Maxmind.com provides a set of GEOIP solutions that simply require a few steps to determine the visitor's country via IP in PHP.
1. Download database and PHP library files
Download GeoID.dat.gz, unzip to GeoIP.dat file.
Download Geoip.inc.
2. Get country information through PHP code
The following is a sample code showing how to obtain the country code and country name.
<?php
//Introduction of PHP library file
include ("Geoip.inc");
Open the local database, and the data is saved in the GeoIP file.
$geoData = Geoip_open (' GeoIP.dat ', geoip_standard);
Access to national IP
$countryCode = geoip_country_code_by_addr ($geoData, $_server[' remote_addr '));
Get the country name
$countryName = geoip_country_name_by_addr ($geoData, $_server[' remote_addr '));
Close Local Database
geoip_close ($geoData);
? >
Differentiate countries by IP in WordPress
Since the use of PHP is no problem, WordPress is certainly Okay. See how I use it.
1. Place database files
Unzip the GeoIP.dat into the WordPress root directory. (You can find wp-config.php or wp-config-sample.php files in this directory)
2. Write the Calling interface
In the theme directory, create a new folder include, and place Geoip.inc in the new folder. and create a new file in this folder geoip.php the contents of the file are 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);
? >
Here only take the country code as the basis for discrimination. And the country code is a global variable, to avoid the page multiple judgments need to repeatedly access GeoIP.dat to obtain information, reduce program overhead.
2. Call the interface, get the country code
3. Open the header.php file and add the code to the top of the file below.
<?php include (' include/geoip.php ');?>
4. Use of country code
Invoke the code in the subject, as shown in the following example.
<?php
global $countryCode;
if ($countryCode = = ' CN ') {
//China Mainland region executes code
} else if ($countryCode = = ' Us ') {
//US region executed code
} else {
//code executed outside the mainland of China and the United States
?>
Someone might ask, "What's the performance of adding such a thing?" Will it require a powerful server? I have tested that the normal server has little impact on the performance of the page loading, you can see the speed of this blog. If you are not at ease, measure yourself.
PS: Using IP Query API interface
Many domestic internet companies such as Tencent, Sina and Taobao have IP query interface, directly call the query can be.
(1) Tencent IP sharing program
Copy Code code as follows:
/** based on Tencent IP sharing program address to obtain IP location, more accurate */function getiploc_qq ($ip 1) {$url = ' http://ip.qq.com/cgi-bin/searchip?searchip1= '. $ ip1 $ch = Curl_init ($url); curl_setopt ($ch, curlopt_encoding, ' gb2312 '); curl_setopt ($ch, Curlopt_timeout,); curl_ Setopt ($ch, Curlopt_returntransfer, true); Get data return $result = Curl_exec ($ch); $result = mb_convert_encoding ($result, "Utf-8", "gb2312"); Code conversion, otherwise garbled curl_close ($ch);p reg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray); $loc = $ Iparray[1];return $loc;}
(2) Sina IP query interface
Copy Code code as follows:
/** based on Sina IP query interface to obtain IP location */function Getiploc_sina ($ip 1) {$url = ' http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format=json&ip= '. $ip 1; $ch = Curl_init ($url);//curl_setopt ($ch, curlopt_encoding, ' UTF8 '); curl_setopt ($ch, Curlopt_timeout, curl_setopt ($ch, Curlopt_returntransfer, true); Get data return $location = Curl_exec ($ch); $location = Json_decode ($location); Curl_close ($ch); $loc = ""; if ($location ===false) return ""; if (Emptyempty ($location->desc)) {$loc = $location->province.$ Location->city. $location->district. $location->isp;} else{$loc = $location->desc;} return $loc;}
(3) using Taobao IP interface
Copy Code code as follows:
/** * According to Taobao IP query interface to obtain IP location */function getcity ($ip) {$url = "http://ip.taobao.com/service/getIpInfo.php?ip=". $ip; $ip =json _decode (file_get_contents ($url)); if ((string) $ip->code== ' 1 ') {return false;} $data = (array) $ip->data;return $ data;}
Summary
Through IP to judge the source of the visitors is very accurate, now some foreign trade websites are through this method to the user to show the distinction, such as the United States, the user defaults to see the United States can buy goods and U.S. logistics information. But not absolutely accurate, such as someone who has been on the wall all year round, he may not have been able to see the country's information. As to whether the need to distinguish between processing, the site is mainly considered.