PHP implements active language jump selection on the homepage

Source: Internet
Author: User
Many websites provide links on the homepage to allow users to select their respective language pages to be visited, Chinese for Chinese, Korean for Korean, and so on. So can we make a program to help you choose? The answer is OK.

Many websites provide links on the homepage to allow users to select their respective language pages to be visited, Chinese for Chinese, Korean for Korean, and so on. So can we make a program to help you choose?

The answer is yes. everyone is using google. you can use a Chinese system to open the google homepage. Naturally, the Chinese homepage is not used in other languages. Because google will determine the preferred language for user system applications.

How can we be as simple as google,

The HTTP Headers Information sent from the browser to the web server contains the Accept-Language Information, which is the Language in the browser tool> Internet Options> convention, it is used to set the browser's acceptable language preferences. it can be a priority column for multiple languages.

The following uses PHP as an example to describe the language information that users can receive in $ _ SERVER ['http _ ACCEPT_LANGUAGE ']. the variable information is similar to 'zh-cn ', for a multi-language column, the title is similar to 'zh-cn, en; q = 0.8, ko; q = 0.5, zh-tw; q = 100.

Error_reporting (E_ALL ^ E_NOTICE );

// Analyze the HTTP_ACCEPT_LANGUAGE attributes
// Here we only use the first language settings (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.