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;
}
?>