Home >Backend Development >PHP Tutorial >Use PHP code to realize automatic judgment and redirection of web pages_PHP tutorial

Use PHP code to realize automatic judgment and redirection of web pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:59:01738browse

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631961.htmlTechArticleThe 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...
Statement:
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