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.
This information is the tool in the browser-> Internet Options-> general language, which is used to set acceptable language preferences of the browser, it can be a priority column in multiple acceptable languages.

The following uses PHP as an example,
The user's acceptable language information is stored in $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE,
The variable information is similar to "zh-cn". For multi-language columns, it is similar to "zh-cn, en; q = 0.8, ko; q = 0.5, zh-tw; q = 0.3"
The following problems can be solved.

<? 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] http://cn.example.com/#/url#');
Break;
Case 'zh-tw ':
Header ('location: [url] http://tw.example.com/#/url#');
Break;
Case 'ko ':
Header ('location: [url] http://ko.example.com/#/url#');
Break;
Default:
Header ('location: [url] http://en.example.com/#/url#');
Break;
}

?>

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.