[英]how to detect internet explorer and firefox using PHP?
如何使用PHP检测Internet Explorer和Firefox?
you can use $_SERVER['HTTP_USER_AGENT']
as found in the php manual . 您可以使用php手册中的
$_SERVER['HTTP_USER_AGENT']
。
However, be aware that this can be changed by the user and some browsers even provide the ability to do this VERY easy (eg Konqueror). 但是,请注意,用户可以更改此设置,某些浏览器甚至提供了非常简单的功能(例如Konqueror)。 A lot of plugins are available to do the same.
许多插件都可以做到这一点。
Never ever trust this string. 永远不要相信这个字符串。
The easiest way is to use PHP's get_browser function, as this will parse the HTTP User-Agent header for you and extract the relevant browser, version, platform, etc. information into an array or object as required. 最简单的方法是使用PHP的get_browser函数,因为它将为您解析HTTP User-Agent标头,然后根据需要将相关的浏览器,版本,平台等信息提取到数组或对象中。
Running this (in array mode for the purposes of this example) will return a data structure in the following format (using the current php_browscap.ini file from the Browser Capabilities Project as of 15th Jan 2011): 运行此命令(出于示例目的,以数组模式运行)将返回以下格式的数据结构(使用截至2011年1月15日的Browser Capabilities Project中的当前php_browscap.ini文件):
Array
(
[browser_name_regex] => �^mozilla/5\.0 \(windows; u; windows nt 6\.1; .*\) applewebkit/.* \(khtml, like gecko\) chrome/8\..* safari/.*$�
[browser_name_pattern] => Mozilla/5.0 (Windows; U; Windows NT 6.1; *) AppleWebKit/* (KHTML, like Gecko) Chrome/8.* Safari/*
[parent] => Chrome 8.0
[browser] => Chrome
[platform] => Win7
[version] => 8.0
[majorver] => 8
[win32] => 1
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[javaapplets] => 1
[javascript] => 1
[cssversion] => 3
[supportscss] => 1
[minorver] => 0
[alpha] =>
[beta] =>
[win16] =>
[win64] =>
[backgroundsounds] =>
[cdf] =>
[vbscript] =>
[activexcontrols] =>
[isbanned] =>
[ismobiledevice] =>
[issyndicationreader] =>
[crawler] =>
[aol] =>
[aolversion] => 0
)
NB: As per the PHP manual page: 注意:根据PHP手册页:
"In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system."
“为了使其正常工作,您在php.ini中的browscap配置设置必须指向系统中browscap.ini文件的正确位置。”
So that's not what you asked for. 所以这不是您要的。 But it's often more senseful to differentiate on features:
但是区分功能通常更有意义:
if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
// Firefox, Safari, Opera, Chrome, IE9
}
else {
// IE5,IE6,IE7,IE8
}
None of the garbage versions of IE supports XHTML for example. 例如,IE的所有垃圾版本都不支持XHTML。 So that's a good way to separate browsers.
因此,这是分隔浏览器的好方法。 Note how IE9 however counts into the newer class, and might actually be treated comparable to Firefox.
请注意,但是IE9如何计入较新的类,并且实际上可能被视为可与Firefox媲美。
To detect Firefox 检测Firefox
$isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') > -1);
To detect IE 检测IE
$isIe = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') > -1);
If you need something just to put some HTML code (like different stylesheet attached) use something like that (instead of server-side code). 如果您只需要放置一些HTML代码(如附加的不同样式表),则使用类似的内容(而不是服务器端代码)。
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
Otherwise use the HTTP_ACCEPT solution because it is based on browser features not just name. 否则,请使用HTTP_ACCEPT解决方案,因为它基于浏览器功能而不仅仅是名称。 Especially that a lot of IE-addons (and some spyware) change some parts of useragent.
特别是许多IE插件(和某些间谍软件)会更改useragent的某些部分。
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$useragent = $_SERVER['HTTP_USER_AGENT'];
}
if (strlen(strstr($useragent , 'MSIE')) > 0) {
$browser = 'internet explorer';
}else if (strlen(strstr($useragent , 'Firefox')) > 0) {
$browser = 'firefox';
}else{
$browser = 'others';
}
echo $browser;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.