PHP-向某些浏览器显示消息
我已经搜索过这个,我发现的一切都超出了我的需要。我以前用 JavaScript 做过这个,但我真的更喜欢使用 PHP。我将如何根据访问者使用的浏览器向他们显示消息?
示例:
IE 用户会看到:“您正在使用 Internet Explorer”
Firefox 用户会看到:“您正在使用 Mozilla Firefox”
我不确定除了 IE、Firefox、Chrome、Safari 之外是否还有其他主要浏览器歌剧。但我至少希望有一条消息分别定向到每个浏览器。谢谢。
I've searched for this and everything I find is way more than I need. I've done this in JavaScript before, but I would really prefer using PHP. How would I go about displaying a message to my visitors, depending on which browser they're using?
Example:
IE User would see: "You're using Internet Explorer"
Firefox User would see: "You're using Mozilla Firefox"
I'm not exactly sure if there are other major browsers besides IE, Firefox, Chrome, Safari, and Opera. But I would at least want to have a message directed to each one of those browsers individually. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要识别用户的浏览器服务器端,您必须解析
$_SERVER['HTTP_USER_AGENT']
变量......或者,可能更好,使用
get_browser
< /a> 函数——请注意,您必须在 php.ini 中配置某些内容,否则您将收到此类警告:就像 PHP 手册页所说:
下载该文件并将此行添加到 php.ini 后:
以下代码部分:
给我:
在 Linux 版 google chrome 的最新版本(4.0.203.2)上 - 好吧,考虑到它是某种夜间构建,我认为它不被识别是正常的......
作为参考,这里是它的用户代理字符串:
这表明 get_
(length=53) 'browser_name_pattern' => string 'Mozilla/5.0 (X11; *Linux*; *rv:1.9*) Gecko/*' (length=44) 'parent' => string 'Mozilla 1.9' (length=11) 'platform' => string 'Linux' (length=5) 'browser' => string 'Mozilla' (length=7) 'version' => string '1.9' (length=3) 'majorver' => string '1' (length=1) 'minorver' => string '9' (length=1) 'alpha' => string '1' (length=1) 'frames' => string '1' (length=1) 'iframes' => string '1' (length=1) 'tables' => string '1' (length=1) 'cookies' => string '1' (length=1) 'javaapplets' => string '1' (length=1) 'javascript' => string '1' (length=1) 'cssversion' => string '2' (length=1) 'supportscss' => string '1' (length=1) 'beta' => string '' (length=0) 'win16' => string '' (length=0) 'win32' => string '' (length=0) 'win64' => string '' (length=0) 'backgroundsounds' => string '' (length=0) 'cdf' => string '' (length=0) 'vbscript' => string '' (length=0) 'activexcontrols' => string '' (length=0) 'isbanned' => string '' (length=0) 'ismobiledevice' => string '' (length=0) 'issyndicationreader' => string '' (length=0) 'crawler' => string '' (length=0) 'aol' => string '' (length=0) 'aolversion' => string '0' (length=1)browser is
很好;但对于某些尖端的测试浏览器来说可能并不完美——不过,我想应该可以与大多数“常见”浏览器一起正常工作......在 firefox 3.5 上;并且:
在 Linux 版 google chrome 的最新版本(4.0.203.2)上 - 好吧,考虑到它是某种夜间构建,我认为它不被识别是正常的......
作为参考,这里是它的用户代理字符串:
这表明 get_
browser is
很好;但对于某些尖端的测试浏览器来说可能并不完美——不过,我想应该可以与大多数“常见”浏览器一起正常工作......To identify the user's browser server-side, you'll have to parse the
$_SERVER['HTTP_USER_AGENT']
variable...... Or, probably better, use the
get_browser
function -- just note you'll have to configure something in php.ini, or you'll get this kind of warning :Like the PHP manual page says :
After downloading that file and adding this line to php.ini :
The following portion of code :
Gives me :
On a recent version (4.0.203.2) of google chrome for Linux -- well, considering it's some kind of nightly build, I suppose it's normal that it's not recognized...
As a reference, here is it's user-agent string :
This show that get_
(length=53) 'browser_name_pattern' => string 'Mozilla/5.0 (X11; *Linux*; *rv:1.9*) Gecko/*' (length=44) 'parent' => string 'Mozilla 1.9' (length=11) 'platform' => string 'Linux' (length=5) 'browser' => string 'Mozilla' (length=7) 'version' => string '1.9' (length=3) 'majorver' => string '1' (length=1) 'minorver' => string '9' (length=1) 'alpha' => string '1' (length=1) 'frames' => string '1' (length=1) 'iframes' => string '1' (length=1) 'tables' => string '1' (length=1) 'cookies' => string '1' (length=1) 'javaapplets' => string '1' (length=1) 'javascript' => string '1' (length=1) 'cssversion' => string '2' (length=1) 'supportscss' => string '1' (length=1) 'beta' => string '' (length=0) 'win16' => string '' (length=0) 'win32' => string '' (length=0) 'win64' => string '' (length=0) 'backgroundsounds' => string '' (length=0) 'cdf' => string '' (length=0) 'vbscript' => string '' (length=0) 'activexcontrols' => string '' (length=0) 'isbanned' => string '' (length=0) 'ismobiledevice' => string '' (length=0) 'issyndicationreader' => string '' (length=0) 'crawler' => string '' (length=0) 'aol' => string '' (length=0) 'aolversion' => string '0' (length=1)browser is
nice ; but maybe not perfect for some cutting-edge test browser -- still, should work fine with most "common" browsers, I suppose...on firefox 3.5 ; and :
On a recent version (4.0.203.2) of google chrome for Linux -- well, considering it's some kind of nightly build, I suppose it's normal that it's not recognized...
As a reference, here is it's user-agent string :
This show that get_
browser is
nice ; but maybe not perfect for some cutting-edge test browser -- still, should work fine with most "common" browsers, I suppose...大多数客户端都会发送 用户代理 字符串,您可以使用 get_browser 将该字符串“翻译”为更“信息丰富”的内容。
但是客户端可以自由地发送它想要的任何字符串,例如将自己标识为 Internet Explorer 的 Opera 浏览器。
Most clients send a user agent string and you can use get_browser to "translate" that string into something more "informative".
But hte client is free to send any string it wants, e.g. opera browsers that identify themselves as internet explorer.
我认为在 PHP 中检测浏览器的唯一方法是来自用户代理,来自 HTTP_USER_AGENT。
或者使用 PHP 函数 get_browser() -> https://www.php.net/function.get-browser
I think the only way to detect browser in PHP is from user agent, from HTTP_USER_AGENT.
Or using PHP function get_browser() -> https://www.php.net/function.get-browser
更多信息请访问 http://www.php.net/function.get-browser
您可以在此处获取 browscap.ini 文件 http://browsers.garykeith.com/downloads.asp
more info at http://www.php.net/function.get-browser
you can get browscap.ini file here http://browsers.garykeith.com/downloads.asp
实际上没有好的方法来确定服务器端的浏览器功能,因为每个用户,无论其浏览器版本如何,都可以操纵浏览器设置,从而获得不同的功能(例如禁用 Java)。
你应该求助于客户端与服务器端相结合,有很多 jscript 库可以做到这一点,例如, 链接文本
There is actually no good way to determine browser capabilities server-side, since each user, regardless to his browser version can manipulate the browser settings thus getting different capabilities(disabling Java for example).
you should resort to client side combined with server side,there are many jscript libraries out there that can do this, for example, link text