Home  >  Article  >  php教程  >  实用举例:利用PHP代码实现网页自动判断转向

实用举例:利用PHP代码实现网页自动判断转向

WBOY
WBOYOriginal
2016-06-21 09:00:581113browse

用户可接受的语言信息,放在$_SERVER['HTTP_ACCEPT_LANGUAGE']里,
变量信息是类似这样的 "zh-cn", 如果是多语言列,是类似 "zh-cn,en;q=0.8,ko;q=0.5,zh-tw;q=0.3"
下面的问题可以迎刃而解了。
代码:

error_reporting(E_ALL ^ E_NOTICE);
// 分析 HTTP_ACCEPT_LANGUAGE 的属性
// 这里只取第一语言设置 (其他可根据需要增强功能,这里只做简单的方法演示)
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;
}
?>



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