如何检测用于访问我网站的浏览器?

时间:2021-12-13 07:16:56

How do I detect what browser (IE, Firefox, Opera) the user is accessing my site with? Examples in Javascript, PHP, ASP, Python, JSP, and any others you can think of would be helpful. Is there a language agnostic way to get this information?

如何检测用户访问我的网站的浏览器(IE,Firefox,Opera)? Javascript,PHP,ASP,Python,JSP以及您能想到的任何其他示例都会有所帮助。是否有与语言无关的方式来获取此信息?

11 个解决方案

#1


10  

If it's for handling the request, look at the User-Agent header on the incoming request.

如果它是用于处理请求,请查看传入请求上的User-Agent标头。

UPDATE: If it's for reporting, configure your web server to log the User-Agent in the access logs, then run a log analysis tool, e.g., AWStats.

更新:如果是用于报告,请将Web服务器配置为在访问日志中记录User-Agent,然后运行日志分析工具,例如AWStats。

UPDATE 2: FYI, it's usually (not always, usually) a bad idea to change the way you're handling a request based on the User-Agent.

更新2:仅供参考,通常(通常并非总是)改变基于用户代理处理请求的方式是个坏主意。

#2


5  

Comprehensive list of User Agent Strings from various Browsers

各种浏览器的用户代理字符串综合列表

The list is really large :)

名单真的很大:)

#3


3  

You would take a look at the User-Agent that they are sending. Note that you can send whatever agent you want, so that's not 100% foolproof, but most people don't change it unless there's a specific reason to.

您可以查看他们发送的用户代理。请注意,你可以发送你想要的任何代理,这不是100%万无一失,但大多数人不会改变它,除非有特定的理由。

#4


1  

A quick and dirty java servlet example

一个快速而肮脏的java servlet示例

private String getBrowserName(HttpServletRequest request) {
    // get the user Agent from request header
    String userAgent = request.getHeader(Constants.BROWSER_USER_AGENT);
    String BrowesrName = "";
    //check for Internet Explorer
    if (userAgent.indexOf("MSIE") > -1) {
        BrowesrName = Constants.BROWSER_NAME_IE;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_FIREFOX) > -1) {
        BrowesrName = Constants.BROWSER_NAME_MOZILLA_FIREFOX;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_OPERA) > -1) {
        BrowesrName = Constants.BROWSER_NAME_OPERA;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_SAFARI) > -1) {
        BrowesrName = Constants.BROWSER_NAME_SAFARI;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_NETSCAPE) > -1) {
        BrowesrName = Constants.BROWSER_NAME_NETSCAPE;
    } else {
        BrowesrName = "Undefined Browser";
    }
    //return the browser name
    return BrowesrName;
}

#5


1  

You can use the HttpBrowserCapabilities Class in ASP.NET. Here is a sample from this link

您可以在ASP.NET中使用HttpBrowserCapabilities类。以下是此链接的示例

private void Button1_Click(object sender, System.EventArgs e)
{
        HttpBrowserCapabilities bc;
        string s;
        bc = Request.Browser;
        s= "Browser Capabilities" + "\n";
        s += "Type = " + bc.Type + "\n";
        s += "Name = " + bc.Browser + "\n";
        s += "Version = " + bc.Version + "\n";
        s += "Major Version = " + bc.MajorVersion + "\n";
        s += "Minor Version = " + bc.MinorVersion + "\n";
        s += "Platform = " + bc.Platform + "\n";
        s += "Is Beta = " + bc.Beta + "\n";
        s += "Is Crawler = " + bc.Crawler + "\n";
        s += "Is AOL = " + bc.AOL + "\n";
        s += "Is Win16 = " + bc.Win16 + "\n";
        s += "Is Win32 = " + bc.Win32 + "\n";
        s += "Supports Frames = " + bc.Frames + "\n";
        s += "Supports Tables = " + bc.Tables + "\n";
        s += "Supports Cookies = " + bc.Cookies + "\n";
        s += "Supports VB Script = " + bc.VBScript + "\n";
        s += "Supports JavaScript = " + bc.JavaScript + "\n";
        s += "Supports Java Applets = " + bc.JavaApplets + "\n";
        s += "Supports ActiveX Controls = " + bc.ActiveXControls + "\n";
        TextBox1.Text = s;
}

#6


1  

PHP's predefined superglobal array $_SERVER contains a key "HTTP_USER_AGENT", which contains the value of the User-Agent header as sent in the HTTP request. Remember that this is user-provided data and is not to be trusted. Few users alter their user-agent string, but it does happen from time to time.

PHP的预定义超全局数组$ _SERVER包含一个键“HTTP_USER_AGENT”,其中包含HTTP请求中发送的User-Agent标头的值。请记住,这是用户提供的数据,不可信任。很少有用户改变他们的用户代理字符串,但它确实会不时发生。

#7


1  

On the client side, you can do this in Javascript using the navigation.userAgent object. Here's a crude example:

在客户端,您可以使用navigation.userAgent对象在Javascript中执行此操作。这是一个粗略的例子:

if (navigator.userAgent.indexOf("MSIE") > -1) 
{
    alert("Internet Explorer!");
}
else if (navigator.userAgent.indexOf("Firefox") > -1)
{
    alert("Firefox!");
}

A more detailed and comprehensive example can be found here: http://www.quirksmode.org/js/detect.html

可以在此处找到更详细和全面的示例:http://www.quirksmode.org/js/detect.html

Note that if you're doing the browser detection for the sake of Javascript compatibility, it's usually better to simply use object detection or a try/catch block, lest some version you didn't think of slip through the cracks of your script. For example, instead of doing this...

请注意,如果您为了Javascript兼容性而进行浏览器检测,通常最好只使用对象检测或try / catch块,以免您想到的某些版本漏掉了脚本的碎片。例如,而不是这样做......

if(navigator.userAgent.indexOf("MSIE 6") > -1)
{
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
    objXMLHttp = new XMLHttpRequest();
}

...this is better:

...这个更好:

if(window.XMLHttpRequest) // Works in Firefox, Opera, and Safari, maybe latest IE?
{
    objXMLHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) // If the above fails, try the MSIE 6 method
{
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

#8


0  

It may be dependent of your setting. With apache on linux, its written in the access log /var/log/apache2/access_log

它可能取决于您的设置。在linux上使用apache,它写在访问日志/ var / log / apache2 / access_log中

#9


0  

You can do this by:
- looking at the web server log, OR
- looking at the User-Agent field in the HTML request (which is a plain text stream) before processing it.

您可以通过以下方式执行此操作: - 查看Web服务器日志,或者 - 在处理HTML请求(纯文本流)之前查看User-Agent字段。

#10


0  

First of all, I'd like to note, that it is best to avoid patching against specific web-browsers, unless as a last result -try to achieve cross-browser compatibility instead using standard-compliant HTML/CSS/JS (yes, javascript does have a common denominator subset, which works across all major browsers).

首先,我想指出,最好避免修补特定的Web浏览器,除非作为最后的结果 - 实现跨浏览器兼容性而不是使用符合标准的HTML / CSS / JS(是的, javascript确实有一个共同的分母子集,适用于所有主流浏览器)。

With that said, the user-agent tag from the HTTP request header contains the client's (claimed) browser. Although this has become a real mess due to people working against specific browser, and not the specification, so determining the real browser can be a little tricky.

话虽如此,HTTP请求标头中的user-agent标签包含客户端(声明的)浏览器。虽然由于人们反对特定的浏览器而不是规范,这已成为一个真正的混乱,因此确定真正的浏览器可能有点棘手。

Match against this:

与此相符:

contains browser

Firefox -> Firefox

Firefox - > Firefox

MSIE -> Internet Explorer

MSIE - > Internet Explorer

Opera -> Opera (one of the few browsers, which don't pretend to be Mozilla :) )

Opera - > Opera(少数浏览器之一,不假装是Mozilla :))

Most of the agents containing the words "bot", or "crawler" are usually bots (so you can omit it from logs / etc)

大多数包含“bot”或“crawler”字样的代理通常都是机器人(所以你可以从日志/等中省略它)

#11


0  

check out browsecap.ini. The linked site has files for multiple scripting languages. The browsecap not only identifies the user-agent but also has info about the browser's CSS support, JS support, OS, if its a mobile browser etc.

看看browsecap.ini。链接的站点包含多种脚本语言的文件。 browsecap不仅识别用户代理,还有关于浏览器的CSS支持,JS支持,操作系统,移动浏览器等的信息。

cruise over to this page to see an example of what info the browsecap.ini can tell you about your current browser.

浏览此页面,查看browsecap.ini可以告诉您当前浏览器的信息示例。

#1


10  

If it's for handling the request, look at the User-Agent header on the incoming request.

如果它是用于处理请求,请查看传入请求上的User-Agent标头。

UPDATE: If it's for reporting, configure your web server to log the User-Agent in the access logs, then run a log analysis tool, e.g., AWStats.

更新:如果是用于报告,请将Web服务器配置为在访问日志中记录User-Agent,然后运行日志分析工具,例如AWStats。

UPDATE 2: FYI, it's usually (not always, usually) a bad idea to change the way you're handling a request based on the User-Agent.

更新2:仅供参考,通常(通常并非总是)改变基于用户代理处理请求的方式是个坏主意。

#2


5  

Comprehensive list of User Agent Strings from various Browsers

各种浏览器的用户代理字符串综合列表

The list is really large :)

名单真的很大:)

#3


3  

You would take a look at the User-Agent that they are sending. Note that you can send whatever agent you want, so that's not 100% foolproof, but most people don't change it unless there's a specific reason to.

您可以查看他们发送的用户代理。请注意,你可以发送你想要的任何代理,这不是100%万无一失,但大多数人不会改变它,除非有特定的理由。

#4


1  

A quick and dirty java servlet example

一个快速而肮脏的java servlet示例

private String getBrowserName(HttpServletRequest request) {
    // get the user Agent from request header
    String userAgent = request.getHeader(Constants.BROWSER_USER_AGENT);
    String BrowesrName = "";
    //check for Internet Explorer
    if (userAgent.indexOf("MSIE") > -1) {
        BrowesrName = Constants.BROWSER_NAME_IE;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_FIREFOX) > -1) {
        BrowesrName = Constants.BROWSER_NAME_MOZILLA_FIREFOX;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_OPERA) > -1) {
        BrowesrName = Constants.BROWSER_NAME_OPERA;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_SAFARI) > -1) {
        BrowesrName = Constants.BROWSER_NAME_SAFARI;
    } else if (userAgent.indexOf(Constants.BROWSER_NAME_NETSCAPE) > -1) {
        BrowesrName = Constants.BROWSER_NAME_NETSCAPE;
    } else {
        BrowesrName = "Undefined Browser";
    }
    //return the browser name
    return BrowesrName;
}

#5


1  

You can use the HttpBrowserCapabilities Class in ASP.NET. Here is a sample from this link

您可以在ASP.NET中使用HttpBrowserCapabilities类。以下是此链接的示例

private void Button1_Click(object sender, System.EventArgs e)
{
        HttpBrowserCapabilities bc;
        string s;
        bc = Request.Browser;
        s= "Browser Capabilities" + "\n";
        s += "Type = " + bc.Type + "\n";
        s += "Name = " + bc.Browser + "\n";
        s += "Version = " + bc.Version + "\n";
        s += "Major Version = " + bc.MajorVersion + "\n";
        s += "Minor Version = " + bc.MinorVersion + "\n";
        s += "Platform = " + bc.Platform + "\n";
        s += "Is Beta = " + bc.Beta + "\n";
        s += "Is Crawler = " + bc.Crawler + "\n";
        s += "Is AOL = " + bc.AOL + "\n";
        s += "Is Win16 = " + bc.Win16 + "\n";
        s += "Is Win32 = " + bc.Win32 + "\n";
        s += "Supports Frames = " + bc.Frames + "\n";
        s += "Supports Tables = " + bc.Tables + "\n";
        s += "Supports Cookies = " + bc.Cookies + "\n";
        s += "Supports VB Script = " + bc.VBScript + "\n";
        s += "Supports JavaScript = " + bc.JavaScript + "\n";
        s += "Supports Java Applets = " + bc.JavaApplets + "\n";
        s += "Supports ActiveX Controls = " + bc.ActiveXControls + "\n";
        TextBox1.Text = s;
}

#6


1  

PHP's predefined superglobal array $_SERVER contains a key "HTTP_USER_AGENT", which contains the value of the User-Agent header as sent in the HTTP request. Remember that this is user-provided data and is not to be trusted. Few users alter their user-agent string, but it does happen from time to time.

PHP的预定义超全局数组$ _SERVER包含一个键“HTTP_USER_AGENT”,其中包含HTTP请求中发送的User-Agent标头的值。请记住,这是用户提供的数据,不可信任。很少有用户改变他们的用户代理字符串,但它确实会不时发生。

#7


1  

On the client side, you can do this in Javascript using the navigation.userAgent object. Here's a crude example:

在客户端,您可以使用navigation.userAgent对象在Javascript中执行此操作。这是一个粗略的例子:

if (navigator.userAgent.indexOf("MSIE") > -1) 
{
    alert("Internet Explorer!");
}
else if (navigator.userAgent.indexOf("Firefox") > -1)
{
    alert("Firefox!");
}

A more detailed and comprehensive example can be found here: http://www.quirksmode.org/js/detect.html

可以在此处找到更详细和全面的示例:http://www.quirksmode.org/js/detect.html

Note that if you're doing the browser detection for the sake of Javascript compatibility, it's usually better to simply use object detection or a try/catch block, lest some version you didn't think of slip through the cracks of your script. For example, instead of doing this...

请注意,如果您为了Javascript兼容性而进行浏览器检测,通常最好只使用对象检测或try / catch块,以免您想到的某些版本漏掉了脚本的碎片。例如,而不是这样做......

if(navigator.userAgent.indexOf("MSIE 6") > -1)
{
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
    objXMLHttp = new XMLHttpRequest();
}

...this is better:

...这个更好:

if(window.XMLHttpRequest) // Works in Firefox, Opera, and Safari, maybe latest IE?
{
    objXMLHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) // If the above fails, try the MSIE 6 method
{
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

#8


0  

It may be dependent of your setting. With apache on linux, its written in the access log /var/log/apache2/access_log

它可能取决于您的设置。在linux上使用apache,它写在访问日志/ var / log / apache2 / access_log中

#9


0  

You can do this by:
- looking at the web server log, OR
- looking at the User-Agent field in the HTML request (which is a plain text stream) before processing it.

您可以通过以下方式执行此操作: - 查看Web服务器日志,或者 - 在处理HTML请求(纯文本流)之前查看User-Agent字段。

#10


0  

First of all, I'd like to note, that it is best to avoid patching against specific web-browsers, unless as a last result -try to achieve cross-browser compatibility instead using standard-compliant HTML/CSS/JS (yes, javascript does have a common denominator subset, which works across all major browsers).

首先,我想指出,最好避免修补特定的Web浏览器,除非作为最后的结果 - 实现跨浏览器兼容性而不是使用符合标准的HTML / CSS / JS(是的, javascript确实有一个共同的分母子集,适用于所有主流浏览器)。

With that said, the user-agent tag from the HTTP request header contains the client's (claimed) browser. Although this has become a real mess due to people working against specific browser, and not the specification, so determining the real browser can be a little tricky.

话虽如此,HTTP请求标头中的user-agent标签包含客户端(声明的)浏览器。虽然由于人们反对特定的浏览器而不是规范,这已成为一个真正的混乱,因此确定真正的浏览器可能有点棘手。

Match against this:

与此相符:

contains browser

Firefox -> Firefox

Firefox - > Firefox

MSIE -> Internet Explorer

MSIE - > Internet Explorer

Opera -> Opera (one of the few browsers, which don't pretend to be Mozilla :) )

Opera - > Opera(少数浏览器之一,不假装是Mozilla :))

Most of the agents containing the words "bot", or "crawler" are usually bots (so you can omit it from logs / etc)

大多数包含“bot”或“crawler”字样的代理通常都是机器人(所以你可以从日志/等中省略它)

#11


0  

check out browsecap.ini. The linked site has files for multiple scripting languages. The browsecap not only identifies the user-agent but also has info about the browser's CSS support, JS support, OS, if its a mobile browser etc.

看看browsecap.ini。链接的站点包含多种脚本语言的文件。 browsecap不仅识别用户代理,还有关于浏览器的CSS支持,JS支持,操作系统,移动浏览器等的信息。

cruise over to this page to see an example of what info the browsecap.ini can tell you about your current browser.

浏览此页面,查看browsecap.ini可以告诉您当前浏览器的信息示例。