检查由PHP浏览器的语言? [英] Checking browser's language by PHP?

查看:138
本文介绍了检查由PHP浏览器的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过PHP检查用户浏览器的语言?

How can I check the language of user's browser by PHP?

我需要显示不同的页面,人们美国和英国。

I need to show a different page for people in US and in UK.

我尝试以下code失败

I tried the following code unsuccessfully

<?php
if(ereg("us", $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
   include('http://page.com/us.txt');
else
   include('http://page.com/uk.txt');
?>

我运行特定的code的人在美国,并为他们在英国。

I run a specific code for people in US and for them in UK.

推荐答案

可能只是一个区分大小写的问题; eregi(EN-US)或preg_match('/ EN-US / I')应该把它捡起来。

Likely just a case sensitivity issue; eregi('en-us') or preg_match('/en-us/i') should have picked it up.

不过,在寻找EN-US在头可能会得到它有时会出错,特别是当列,美国和英国的语言。 接受语言,实际上是一个相当复杂的头,这真的是你会想要一个适当的解析器。

However, just looking for ‘en-us’ in the header may get it wrong sometimes, in particular when both the US and UK languages are listed. "Accept-Language" is actually quite a complicated header, which really you'd want a proper parser for.

如果您有PECL整个工作已经为你做的:<一href=\"http://www.php.net/manual/en/function.http-negotiate-language.php\">http://www.php.net/manual/en/function.http-negotiate-language.php

If you have PECL the whole job is already done for you: http://www.php.net/manual/en/function.http-negotiate-language.php

我不知道为什么其他的答案会为User-Agent头;这是完全伪造的。用户代理未授权任何特别的地方举行一个语言的价值,并为一些浏览器(如Opera和一些小的浏览器我从来没有听说过所谓的Internet Explorer的的),它不会在所有。它的确实的包含一种语言,那将是该语言的浏览器版本安装在,这就是你应该看不是用户的preferred语言。 (此设置将默认为构建语言,但可以通过从preferences UI用户自定义)。

I don't know why the other answers are going for the User-Agent header; this is utterly bogus. User-Agent is not mandated to hold a language value in any particular place, and for some browsers (eg. Opera, and some minor browser I've never heard of called ‘Internet Explorer’) it will not at all. Where it does contain a language, that'll be the of language the browser build was installed in, not the user's preferred language which is what you should be looking at. (This setting will default to the build language, but can be customised by the user from the preferences UI.)

这篇关于检查由PHP浏览器的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆