What's the use of distinguishing visitor countries?

Here are a few examples of how I use the function.

1. Differentiate website functions

This blog has the function of translating articles, This is for the convenience of overseas visitors to read the article, But it seems very redundant to the Chinese. So I judge the country by ip, Block translation function in mainland China.

2. Differentiate display advertising

For example, in the mainland of China, at the bottom of the sidebar, you see the advertisement of Nafuneng. And other areas see ads from Google. Hostucan is one of my advertisers, English website, There are also Chinese websites, So I can provide him with differentiated display services, Avoid wasting traffic.

3. Shield layout service

There are many good service platforms overseas, You can collect data and share articles by placing points on the website. But unfortunately, For some reason, They don't show well in China. Not only did not work as expected, It also makes the page load time longer. These layouts can be blocked for mainland visitors.

Differentiate countries by ip in php

How to use php to distinguish countries and regions by ip?Maxmind.com provides a set of geoip solutions, It only takes a few simple steps to determine the country of a visitor by ip in php.

1. Download the database and php library files

Downloadgeoid.dat.gz, Extract to geoip.dat file.


2. Get country information via php code

The following is a sample code, Show me how to get the country code and country name.

//import php library files
include ("geoip.inc");
//open local database, The data is saved in a geoip file.
$geodata=geoip_open ("geoip.dat", geoip_standard);
//get country ip
$countrycode=geoip_country_code_by_addr ($geodata, $_server ["remote_addr"]);
//get country name
$countryname=geoip_country_name_by_addr ($geodata, $_server ["remote_addr"]);
//close the local database
geoip_close ($geodata);

Differentiate countries by ip in wordpress

Since it works fine on php, Wordpress is definitely okay. See how I use it.

Place the database file

Extract geoip.dat into the wordpress root directory. (You can find wp-config.php or wp-config-sample.php file in this directory)

2. Writing the call interface

Create a new folder include in the theme directory and place geoip.inc in the new folder. And create a new file geoip.php file content in this folder as follows.

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 the national code is used as the basis for discrimination. And the country code is a global variable, To avoid multiple judgments on the page, you need to repeatedly visit geoip.dat to obtain information. Reduce program overhead.

2. Call the interface, Get country code

3. Open the header.php file and add the following code at the top of the file.

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

4. Use country code

Calling code in the theme, Examples are as follows.

global $countrycode;
if ($countrycode == "cn") {
 //Code executed in mainland China
} else if ($countrycode == "us") {
 //Code executed in the US
} else {
 //Code executed in mainland China and outside the United States

Someone may ask, Add such a thing, How about performance?Will it require a powerful server?I have tested, Page load performance is hardly affected on a normal server, Take a look at the speed of this blog. If you don't worry, Test it yourself.

ps:use ip query api interfaceMany domestic Internet companies such as Tencent, Sina and Taobao have ip query interfaces.Simply call the query.

(1) Tencent IP sharing plan

/** Acquire the IP location based on the address of Tencent's IP sharing plan, which is more accurate . $ip1;$ch=curl_init ($url);curl_setopt ($ch, curlopt_encoding, "gb2312");curl_setopt ($ch, curlopt_timeout, 10);curl_setopt ($ch, curlopt_returntransfer, true);//Get data return $result=curl_exec ($ch);$result=mb_convert_encoding ($result, "utf-8", "gb2312");//encoding conversion,Otherwise garbled curl_close ($ch);preg_match ("@ & lt;span > (. *)</span></p>@iu", $result, $iparray);$loc=$iparray [1];return $loc;}

(2) Sina IP query interface

/** Get the IP address according to the Sina IP query interface */function getiploc_sina ($ip1) {$url="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip= ". $ip1;$ch=curl_init ($url);//curl_setopt ($ch, curlopt_encoding," utf8 ");curl_setopt ($ch, curlopt_timeout, 10);curl_setopt ($ch, curlopt_returntransfer, true);//Get data and 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) Use Taobao IP interface

/** * Get the IP address according to the Taobao ip query interface */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 ;}

to sum up

It is very accurate to judge the source of visitors by ip, Now some foreign trade websites use this method to display and distinguish users. For example, users in the United States see by default the products that can be purchased in the United States and U.S. logistics information. But not 100%accurate, For example, someone crosses the wall all year round, Then he may never see the information of his country. As for whether it needs to be treated separately, The website is mainly considered.

  • Previous Spring mvc receives json object
  • Next SpringMVC restful annotation @RequestBody for json and object conversion