Home > Backend Development > PHP Tutorial > Let your website homepage automatically select the language to jump_PHP tutorial

Let your website homepage automatically select the language to jump_PHP tutorial

WBOY
Release: 2016-07-21 15:59:45
Original
858 people have browsed it

Everyone is using Google. If you use the Chinese system to open the Google homepage, the Chinese homepage will naturally open, not other languages. Because Google will automatically determine what is the preferred language used by the user's system.
How can we do it like Google? It is actually very simple.
The HTTP Headers Information sent by the browser to the web server contains such information Accept-Language
This information is the tool in the browser ->Internet Options->Language under General, it is used to set the language preferences acceptable to the browser. It can be a priority ordering column for multiple acceptable languages.

The following takes PHP as an example.
The language information acceptable to the user is placed in $_SERVER['HTTP_ACCEPT_LANGUAGE'].
The variable information is similar to "zh-cn". If it is The multi-language column is similar to "zh-cn,en;q=0.8,ko;q=0.5,zh-tw;q=0.3"
The following problems can be easily solved.


Program code


error_reporting(E_ALL ^ ​​E_NOTICE);

// Analyze the attributes of HTTP_ACCEPT_LANGUAGE
/ / Only the first language setting is taken here (other functions can be enhanced as needed, here is only a simple method demonstration)

preg_match('/^([a-z-]+)/i', $_SERVER[' HTTP_ACCEPT_LANGUAGE'], $matches);
$lang = $matches[1];

switch ($lang) {
case 'zh-cn' :
header('Location: http://cn.example.com/');
break;
case 'zh-tw' :
header('Location: http://tw.example.com/');
break;
case 'ko' :
header('Location: http://ko.example.com/');
break;
default:
header('Location : http://en.example.com/');
break;
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317293.htmlTechArticleEveryone is using Google. If you use the Chinese system to open the Google homepage, the Chinese homepage will naturally open, not the Chinese homepage. There will be other languages. Because Google will automatically determine the preferred language used by the user's system...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template