Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    FUNCTION: Browser Name/Version detection

    PHP Code:
    <?php

    /*
    Browser Name/Version detection
    By Geoffrey Sneddon, with some help from Clayton Smith
    Released under the zlib/libpng license
    */

    function browser($ua)
    {
        if (
    preg_match('/bot/i'$ua) || preg_match('/crawl/i'$ua) || preg_match('/yahoo\!/i'$ua))
        {
            
    $return['name'] = 'Bot';
            
    $return['version'] = 'Unknown';
        }
        elseif (
    preg_match('/opera/i'$ua))
        {
            
    preg_match('/Opera(\/| )([0-9\.]+)(u)?(\d+)?/i'$ua$b);
            
    $return['name'] = 'Opera';
            unset(
    $b[0], $b[1]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/msie/i'$ua))
        {
            
    preg_match('/MSIE ([0-9\.]+)(b)?/i'$ua$b);
            
    $return['name'] = 'Internet Explorer';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/omniweb/i'$ua))
        {
            
    preg_match('/OmniWeb\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'OmniWeb';
            if (isset(
    $b[1]))
                
    $return['version'] = $b[1];
            else
                
    $return['version'] = 'Unknown';
        }
        elseif (
    preg_match('/icab/i'$ua))
        {
            
    preg_match('/iCab\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'iCab';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/safari/i'$ua))
        {
            
    preg_match('/Safari\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Safari';
            
    $return['version'] = $b[1];
            switch (
    $return['version'])
            {
                case 
    '412':
                case 
    '412.2':
                case 
    '412.2.2':
                    
    $return['version'] = '2.0';
                break;
                case 
    '412.5':
                
    $return['version'] = '2.0.1';
                break;
                case 
    '416.12':
                case 
    '416.13':
                    
    $return['version'] = '2.0.2';
                break;
                case 
    '100':
                    
    $return['version'] = '1.1';
                break;
                case 
    '100.1':
                    
    $return['version'] = '1.1.1';
                break;
                case 
    '125.7':
                case 
    '125.8':
                    
    $return['version'] = '1.2.2';
                break;
                case 
    '125.9':
                    
    $return['version'] = '1.2.3';
                break;
                case 
    '125.11':
                case 
    '125.12':
                    
    $return['version'] = '1.2.4';
                break;
                case 
    '312':
                    
    $return['version'] = '1.3';
                break;
                case 
    '312.3':
                case 
    '312.3.1':
                    
    $return['version'] = '1.3.1';
                break;
                case 
    '85.5':
                    
    $return['version'] = '1.0';
                break;
                case 
    '85.7':
                    
    $return['version'] = '1.0.2';
                break;
                case 
    '85.8':
                case 
    '85.8.1':
                    
    $return['version'] = '1.0.3';
                break;
            }
        }
        elseif (
    preg_match('/konqueror/i'$ua))
        {
            
    preg_match('/Konqueror\/([0-9\.]+)(\-rc)?(\d+)?/i'$ua$b);
            
    $return['name'] = 'Konqueror';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/Flock/i'$ua))
        {
            
    preg_match('/Flock\/([0-9\.]+)(\+)?/i'$ua$b);
            
    $return['name'] = 'Flock';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/firebird/i'$ua))
        {
            
    preg_match('/Firebird\/([0-9\.]+)(\+)?/i'$ua$b);
            
    $return['name'] = 'Firebird';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/phoenix/i'$ua))
        {
            
    preg_match('/Phoenix\/([0-9\.]+)(\+)?/i'$ua$b);
            
    $return['name'] = 'Phoenix';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/firefox/i'$ua))
        {
            
    preg_match('/Firefox\/([0-9\.]+)(\+)?/i'$ua$b);
            
    $return['name'] = 'Firefox';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/chimera/i'$ua))
        {
            
    preg_match('/Chimera\/([0-9\.]+)(a|b)?(\d+)?(\+)?/i'$ua$b);
            
    $return['name'] = 'Chimera';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/camino/i'$ua))
        {
            
    preg_match('/Camino\/([0-9\.]+)(a|b)?(\d+)?(\+)?/i'$ua$b);
            
    $return['name'] = 'Camino';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/seamonkey/i'$ua))
        {
            
    preg_match('/SeaMonkey\/([0-9\.]+)(a|b)?/i'$ua$b);
            
    $return['name'] = 'SeaMonkey';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/galeon/i'$ua))
        {
            
    preg_match('/Galeon\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Galeon';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/epiphany/i'$ua))
        {
            
    preg_match('/Epiphany\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Epiphany';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/mozilla\/5/i'$ua) || preg_match('/gecko/i'$ua))
        {
            
    preg_match('/rv(:| )([0-9\.]+)(a|b)?/i'$ua$b);
            
    $return['name'] = 'Mozilla';
            unset(
    $b[0], $b[1]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/mozilla\/4/i'$ua))
        {
            
    preg_match('/Mozilla\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Netscape';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/lynx/i'$ua))
        {
            
    preg_match('/Lynx\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Lynx';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/links/i'$ua))
        {
            
    preg_match('/Links \(([0-9\.]+)(pre)?(\d+)?/i'$ua$b);
            
    $return['name'] = 'Links';
            unset(
    $b[0]);
            
    $return['version'] = implode(''$b);
        }
        elseif (
    preg_match('/curl/i'$ua))
        {
            
    preg_match('/curl\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'cURL';
            
    $return['version'] = $b[1];
        }
        elseif (
    preg_match('/wget/i'$ua))
        {
            
    preg_match('/Wget\/([0-9\.]+)/i'$ua$b);
            
    $return['name'] = 'Wget';
            
    $return['version'] = $b[1];
        }
        else
        {
            
    $return['name'] = 'Unknown';
            
    $return['version'] = 'Unknown';
        }
        return 
    $return;
    }

    ?>
    Grabbed straight out Fstats source. Like the rest of the Fstats, released under the zlib/libpng license:
    This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
    Last edited by gsnedders; 12-08-2005 at 07:12 PM. Reason: I can't even spell function in the title

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    This is a usefull function for having seperate content for different user. Like if one template contains code not handled by another browser, browser() will allow you to display an alternative template for that user, and so fourth.

    Also, Error 404, can you show some examples of ways to use it? Look like it should have alot of methods that could be done.

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Element
    Also, Error 404, can you show some examples of ways to use it? Look like it should have alot of methods that could be done.
    Sure.

    PHP Code:
    <?php

    $ua 
    browser($_SERVER['HTTP_USER_AGENT']);
    echo 
    "You are using version $ua[version] of $ua[name]";

    ?>
    Example output:
    You are using version 2.0.2 of Safari
    You are using version 1.5 of Firefox
    I'll post an function for just browser detection, without the version.

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Browser name detection only:

    PHP Code:
    <?php

    /*
    Browser Name detection
    By Geoffrey Sneddon, with some help from Clayton Smith
    Released under the zlib/libpng license
    */

    function browser($ua)
    {
        if (
    preg_match('/bot/i'$ua) || preg_match('/crawl/i'$ua) || preg_match('/yahoo\!/i'$ua))
        {
            return 
    'Bot';
        }
        elseif (
    preg_match('/opera/i'$ua))
        {
            return 
    'Opera';
        }
        elseif (
    preg_match('/msie/i'$ua))
        {
            return 
    'Internet Explorer';
        }
        elseif (
    preg_match('/omniweb/i'$ua))
        {
            return 
    'OmniWeb';
        }
        elseif (
    preg_match('/icab/i'$ua))
        {
            return 
    'iCab';
        }
        elseif (
    preg_match('/safari/i'$ua))
        {
            return 
    'Safari';
        }
        elseif (
    preg_match('/konqueror/i'$ua))
        {
            return 
    'Konqueror';
        }
        elseif (
    preg_match('/Flock/i'$ua))
        {
            return 
    'Flock';
        }
        elseif (
    preg_match('/firebird/i'$ua))
        {
            return 
    'Firebird';
        }
        elseif (
    preg_match('/phoenix/i'$ua))
        {
            return 
    'Phoenix';
        }
        elseif (
    preg_match('/firefox/i'$ua))
        {
            return 
    'Firefox';
        }
        elseif (
    preg_match('/chimera/i'$ua))
        {
            return 
    'Chimera';
        }
        elseif (
    preg_match('/camino/i'$ua))
        {
            return 
    'Camino';
        }
        elseif (
    preg_match('/seamonkey/i'$ua))
        {
            return 
    'SeaMonkey';
        }
        elseif (
    preg_match('/galeon/i'$ua))
        {
            return 
    'Galeon';
        }
        elseif (
    preg_match('/epiphany/i'$ua))
        {
            return 
    'Epiphany';
        }
        elseif (
    preg_match('/mozilla\/5/i'$ua) || preg_match('/gecko/i'$ua))
        {
            return 
    'Mozilla';
        }
        elseif (
    preg_match('/mozilla\/4/i'$ua))
        {
            return 
    'Netscape';
        }
        elseif (
    preg_match('/lynx/i'$ua))
        {
            return 
    'Lynx';
        }
        elseif (
    preg_match('/links/i'$ua))
        {
            return 
    'Links';
        }
        elseif (
    preg_match('/curl/i'$ua))
        {
            return 
    'cURL';
        }
        elseif (
    preg_match('/wget/i'$ua))
        {
            return 
    'Wget';
        }
        else
        {
            return 
    'Unknown';
        }
    }

    ?>
    Grabbed straight out Fstats source. Like the rest of the Fstats, released under the zlib/libpng license:
    This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
    Example:
    PHP Code:
    <?php
    if (browser($_SERVER['HTTP_USER_AGENT']) == 'Internet Explorer')
    echo 
    'You are using Internet Explorer.';
    ?>

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    This is deffinately very usefull. I like the first example, it has both outputs and you can still limit by browser, for example:

    PHP Code:
    <?php

    $browser 
    browser($_SERVER['HTTP_USER_AGENT']);
    if(
    $browser['name'] == 'Internet Explorer' || $browser['version'] == '4.0.0') { // Not sure if it would come out 4.0 or 4.0.0
      
    die("We're sorry, currently this website does not support ".$browser['name'] . " " $browser['version'] .". Please try back in a few weeks.");
    }

    ?>

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Remember, this is never a sure way to get information about a browser. For example, in opera, you have a quick menu that has options "Identify as Internet Explorer", "Identify as Mozilla" and "Identify as Opera".

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Indeed...that is the the way I take the approach to programming PHP...do not trust the client in the slightest. Anything is suspect coming from the client, though IP addresses are generally harder to spoof, but possible. Their email address could be fake, their session id may have been hijacked, there could be an attempted SQL injection in the form value, there could be someone trying to override an uninitialized variable through $_GET with register_globals on, and so on. The data from this function is useful, yes, but don't trust it.
    "$question = ( to() ) ? be() : ~be();"

  • #8
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I would say its safe to say you can assume to trust the information. Most people don't mess with their browsers much however there are tons of browsers out there that won't even register on this, returning "Unknown".
    I talked to my friend who works for some computer place installing computer parts and coding their websites and such. He says in most web design you should always assume a user has common settings, if not, its they're own problem they're not supported on the website. Though he also says he doesn't use PHP for browser detection, or JavaScript. (Don't know what that leaves, maybe ASP or CGI?)

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When you first install Opera, it is marked as IE, you have to manually change it to Opera. The point I'm making is, is you should never rely on this information, or, as Velox Letum rightly said, any information obtained from the client. You are right in saying that most people dont mess with their browsers... however, its the ones that do who will cause you the trouble.

  • #10
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by missing-score
    When you first install Opera, it is marked as IE, you have to manually change it to Opera. The point I'm making is, is you should never rely on this information, or, as Velox Letum rightly said, any information obtained from the client. You are right in saying that most people dont mess with their browsers... however, its the ones that do who will cause you the trouble.
    Opera's UA string is
    Mozilla/4.0 (compatible; MSIE 6.0; Mac_PowerPC Mac OS X; en) Opera 8.51
    Under my script, it always checks for Opera before MISE, so it'll show up as Opera.

    As Velox Letum said, never trust any info from the client. It can always be faked.

  • #11
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I found this today and it might be more absolute then this functoin. Go ahead and try it if you want.

    get_browser()

    Array
    (
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [css] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
    )
    Example:

    PHP Code:
     
     
    $browser
    =get_browser($_SERVER['HTTP_USER_AGENT']);
     
    echo 
    $browser['browser']."<br />";
    echo 
    $browser['version'];
     
    // OR
     
    echo $browser->browser;
    echo 
    $browser->version
    Last edited by Element; 12-17-2005 at 02:18 AM.

  • #12
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    get_browser() isn't enabled by default, which is a problem.

    It is undoubtedly more accurate as it has thousands of UA strings.

  • #13
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Error 404
    get_browser() isn't enabled by default, which is a problem.

    It is undoubtedly more accurate as it has thousands of UA strings.
    Yeah, I found that out after the fact. But Velox installed it and it is pretty cool. Example at http://amerikanmetz.rave5.com/misc/tracker2.php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •