내 사이트에 액세스하는 데 사용되는 브라우저를 어떻게 감지합니까?

StackOverflow https://stackoverflow.com/questions/121280

  •  02-07-2019
  •  | 
  •  

문제

사용자가 내 사이트에 액세스하는 브라우저 (예 : Firefox, Opera)를 어떻게 감지합니까? JavaScript, PHP, ASP, Python, JSP의 예 및 당신이 생각할 수있는 다른 모든 것의 예는 도움이 될 것입니다. 이 정보를 얻는 언어 불가지론 적 방법이 있습니까?

도움이 되었습니까?

해결책

요청을 처리하기위한 경우 사용자 에이전트 들어오는 요청에 대한 헤더.

업데이트 :보고하기위한 경우 웹 서버를 액세스 로그에서 사용자 에이전트를 기록하도록 구성한 다음 로그 분석 도구 (예 :)를 실행하십시오. Awstats.

업데이트 2 : FYI 대개 (항상, 일반적으로) 사용자 에이전트를 기반으로 요청을 처리하는 방식을 변경하는 나쁜 생각입니다.

다른 팁

당신은 사용자 에이전트 그들이 보내는 것. 원하는 에이전트를 보낼 수 있으므로 100% 멍청하지는 않지만 대부분의 사람들은 특정한 이유가 없다면 변경하지 않습니다.

빠르고 더러운 자바 서블릿 예제

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;
}

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;
}

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.

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

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

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...

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");
}

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

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.

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).

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.

Match against this:

contains browser

Firefox -> Firefox

MSIE -> Internet Explorer

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

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

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.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top