PHP automatically selects the language to jump to the homepage

Source: Internet
Author: User
Many websites provide links on the homepage to allow users to select their respective language pages, Chinese as the Chinese, and Korean as the Korean. So can I make a program to automatically help with the selection?
The answer is yes. Everyone is using google. If you open google's homepage in a Chinese system, you will naturally open the Chinese homepage instead of other languages. Because google will automatically determine the preferred language used by the user system.
How can we make it as simple as google,
The HTTP Headers Information sent from the browser to the web server contains the Accept-Language Information, which is the tool in the browser-> Internet Options-> common Language, it is used to set the browser's acceptable language preference. It can be a priority column for multiple acceptable languages.
The following uses PHP as an example. The acceptable language information is put in $ _ SERVER ['http _ ACCEPT_LANGUAGE ']. The variable information is similar to "zh-cn ", for multi-language columns, the following problems can be solved, such as "zh-cn, en; q = 0.8, ko; q = 0.5, zh-tw; q = 0.3.

<? Php
Error_reporting (E_ALL ^ E_NOTICE );
// Analyze the HTTP_ACCEPT_LANGUAGE attributes
// Here, only the first language settings are used (other functions can be enhanced as needed. Here, we will only make a simple demonstration)
Preg_match ('/^ ([a-z-])/I', $ _ SERVER ['http _ ACCEPT_LANGUAGE '], $ matches );
$ Lang = $ matches [1];
Switch ($ lang ){
Case 'zh-cn ':
Header ('Location: [url]');
Case 'zh-tw ':
Header ('Location: [url]');
Case 'Ko ':
Header ('Location: [url]');
Header ('Location: [url]');

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.