Differentiate the country and language information of the accessed user based on the HTTP Request Information

Source: Internet
Author: User

Today, we will introduce how to identify different countries. It takes only a few steps to identify requests from different countries and make your web applications more international.
Country recognition mainly uses host, accept-language, Cookie, request URL, IP address, and so on in the HTTP header.

Next, let's take a look at the basic knowledge of HTTP headers.

1. HTTP Header Format

In web development, both the frontend and backend send requests to browse the Web page. In development, we often need to identify client information, such as country, region, and browser information. The information is sent by the HTTP header.
The following describes the HTTP headers used by the company to identify the country version of the client in the extended international business.

1.1.1 HTTP headers

HTTP headers is the core of HTTP requests. It carries information about the client browser, request page, server, and so on.

1.1.2 host

The domain name of the requested web server.
For example, the web request URL: http: // test. Baidu.com/test/
The host is test.baidu.com.
In PHP, you can view it through $ _ server ['HTTP _ host'] or $ _ server ['server _ name.

1.1.3 accept-language

Accept-language: En-US, en; q = 0.5
This information indicates the user's default language settings. If the website has different language versions, you can use this information to redirect your browser.
It can be separated by commas to carry multiple languages. The first language will be the preferred language. Other languages will carry a "Q" value to indicate the user's preference for the language (0 ~ 1 ).
Use $ _ server ["http_accept_language"] in PHP to obtain this information.

1.1.4 cookie

The cookie information stored in your browser is sent to the server.
COOKIE: PHPSESSID = r2t5uvjq435r4q7ib3vtdjq120; Foo = bar
It is a group of name-value pairs separated by semicolons. The cookie can also contain the session ID.
In PHP, a single cookie can be obtained by accessing the $ _ cookie array. You can directly use $ _ Session array to obtain the session variable. If you need a session ID, you can use the session_id () function to replace the cookie.

2. Country version recognition

We have introduced several common HTTP headers. Now you are sure you can try to identify these country versions.

2.1.1 specifications

Before introducing the example, first understand the known norms in country recognition.

The language name indicates the language version corresponding to the current request and complies with the bcp47 protocol. (Online validation: http://schneegans.de/lv? Tags = ZH-CN & format = text)

Standard provisions: the tag is expressed in lower case in the first part, and the second part is the region and upper case. Separated by a hyphen.
The following lists several common abbreviated standard tags:

2.1.2 domain (Domain Name) Method

This method is the domain name requested by the client. If the language under the corresponding domain name is supported, it is successfully recognized.

The domain name here is the host request in the header, which is obtained in PHP through $ _ server ['server _ name.

For example, we browse the Web site: http://www.xxx.com.eg

Home Page, look at the requests sent by the browser (in windows, the Firefox browser is used as an example ),

As you can see, the host in the header is the current domain name, we can identify it by following the steps below:
1. Configure the correspondence between the current domain name and the country version on the backend.
2. In the PHP program on the server side, use $ _ server ['server _ name'] to obtain the host in the header.
3. Find the matching country version (region) based on the corresponding relationship in step 1, and find the language through the locale correspondence.


URL & cookie Method
The principles of this method are as follows:

1) URL Method

In this way, the country version is determined by passing in language parameters in the URL.
The application scenario is like accessing the Domain Name of the Egyptian country. The default value you view is Arabic. If you want to use other languages to browse the pages under the Egyptian domain name (such as English), you need to use the URL method.

For example we browse Web site: http://www.xxx.com.eg? Locale = en-eg.

The priority of the country language information transmitted by URL is higher than that of other methods such as host mode, which is the highest priority.

2) Cookie Method

Identify the version by sending the cookie information (locale) on the request page.

In this way, after the client browses the page for the first time, the server confirms the national language version through the host method, and then sets the cookie on the client. The HTTP header sent in the following browser request carries the cookie information.

The following is an example of the cookie process:

The HTTP response header is as follows:

As shown in, after the first request, the client cookie is set to ar-eg.
The cookie is contained in the request header sent by the client.

The process for identifying the server is as follows:
1. Obtain the cookie in the request. If there is country language information, perform the following steps:
2. Determine whether the obtained country language information is currently supported. If supported, the system returns the version in the language. The default language version is not supported.

2.1.4 IP address Method

We know that IP addresses can be used to identify hosts in the network. In addition, IP address segments are organized regularly, and each country has different IP address segments.
Then we can get the IP address of the client, identify the country and region through the IP address library, and set the language according to the country.
The identification process of this method is as follows:
1. Obtain the IP address of the Request Client
2. determine the country based on the IP address, and display it back to the client using the default language of the country (which can be used together with other methods to identify specific languages, such as accept_lanuage explicitly indicating the request Language and giving priority to the language ).

2.1.5 accept_language Method

"Accept_language" is an optional HTTP header defined for section 1.1 of HTTP/14.4 of rfc2616. It is usually used to declare a language preference that represents the user's receiving language.
When the client requests, the default accept_language is sent. The server obtains the value. If the value is supported, the version of the language is returned.

The identification process of this method is as follows:
1. Obtain the accept_language of the request Client
2. Determine whether the obtained country language information is currently supported. If supported, the system returns the version in the language. The default language version is not supported.
The disadvantage of this method is that it is related to the version of the browser used by the user.
For example, if the browser uses Firefox of the Chinese version, the header request is automatically added when it is sent.
Accept-language ZH-CN.
As shown in the request for the Chinese version of Firefox, if you have browsers of multiple language versions, different languages will appear in different versions of browsers. (For example, you can browse and display the Chinese interface in the Chinese version of Firefox, and view and display the English interface in the English version of Firefox)
This scenario has a high probability of appearance in bilingual countries. In development, You can flexibly set the low priority of this recognition method based on your needs (below is a detailed introduction of the priority ).

2.1.6 priority

You may ask, when I send a header request, there are host, Cookie, URL, accept_language, and IP address at the same time.
What is the final way to confirm the country language of the client request?
You need to set the priority of each method. The general sequence of international recognition can be as follows:
1) URL Method
2) Cookie Method
3) Domain Method
4) accept_language
5) IP Address
So why is the priority above?
The URL method is a situation where the user explicitly switches the language and must have the highest priority. Cookie, as the second one, can reduce the recognition logic and make it easier to identify. The complex recognition logic is only used when the client requests for the first time.
Of course, you can set priority based on your needs.

3 conclusion

These methods can be used flexibly in the development of large web programs to identify the language information of foreign countries, and then develop web applications that support multiple languages.

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.