Home >Backend Development >PHP Tutorial >Use PHP code to realize automatic judgment and redirection of web pages_PHP tutorial
The language information acceptable to the user is placed in $_SERVER['HTTP_ACCEPT_LANGUAGE']. The variable information is similar to this "zh-cn". If it is a multi-language column, it 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.
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;
}
?>