How to determine which browser is used to access my site?

How to determine which browser (IE, Firefox, Opera) the user accesses my site? Examples in Javascript, PHP, ASP, Python, JSP and any others you might think of would be helpful. Is there an agnostic way to get this information?

+2
source share
11 answers

If you are processing a request, review the User-Agent header of the incoming request.

UPDATE: if it is for reporting, configure your web server to register the User-Agent in access logs, then run a log analysis tool such as AWStats .

2: FYI, ( ), User-Agent.

+10
+5

User-Agent, . , , , 100% , , .

+3

Java

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

HttpBrowserCapabilities ASP.NET.

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

PHP $_SERVER "HTTP_USER_AGENT", User-Agent, HTTP-. , , , . , .

+1

Javascript, navigation.userAgent. :

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

: http://www.quirksmode.org/js/detect.html

, Javascript, try/catch, - script. , ...

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

... :

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

. apache linux /var/log/apache2/access_log

0

:
- -,
- User-Agent HTML- ( ) .

0

, , -, HTML/CSS/JS (, javascript , ).

HTTP- () . - , , , .

:

Firefox → Firefox

MSIE → Internet Explorer

Opera → Opera ( , Mozilla:))

, "" "", ( /etc)

0

browsecap.ini. . , CSS , JS, , ..

, , browsecap.ini .

0

Source: https://habr.com/ru/post/1016545/


All Articles