Display Users Browser

Discussion in 'Programming' started by iMatt, Dec 7, 2007.

Thread Status:
Not open for further replies.
  1. iMatt New Member

    Member Since:
    Nov 11, 2007
    Message Count:
    50
    Likes Received:
    0
    Occupation:
    Student
    Location:
    Manchester, UK
    Display Users Browsers

    Here is a small function to detect a users browser, and display a browser icon, if you have any. If you don't have any icons, or just don't want to use the feature, you can of course turn it off.

    Source Code:
    PHP:
    <?php

    function defineUserAgent($browser, $icon = true) {

    $browserIcons = array(

    'Mozilla Firefox' => './firefox.png', // Mozilla Firefox Icon Location
    'Internet Explorer' => './explorer.png', // Internet Explorer Icon location
    'Opera' => './opera.png', // Opera Icon Locations
    'Netscape' => './netscape.png' // Netscape Icon Location

    );

    $browserIndex = array(

    'Mozilla Firefox' => '/(firebird|firefox|gecko)/i',
    'Opera' => '/opera/i',
    'Netscape' => '/(netscape|mozilla)/i',
    'Safari' => '/safari/i',
    'Internet Explorer' => '/\msie/i',
    'SearchBot' => '/(nuhk|googlebot|yammybot|openbot|slurp/cat|msnbot|ia_archiver)/i',
    'OmniWeb' => '/omniweb/i',
    'Flock' => '/flock/i',
    'Links' => '/links/i',
    'cURL' => '/curl/i',
    'Galeon' => '/galeon/i',
    'Epiphany' => '/epiphany/i',
    'Lynx' => '/lynx/i',
    'Wget' => '/wget/i',
    'Phoenix' => '/phoenix/i',
    'iCab' => '/icab/i',
    'Camino' => '/camino/i',
    'SeaMonkey' => '/seamonkey/i',
    'Konqueror' => '/konqueror/i',
    'Chimera' => '/chimera/i',
    'MyIE' => '/myie/i'

    );


    foreach($browserIndex as $browserName => $pattern) {

    if(preg_match($pattern, $browser)) {

    $browserIcon = '';
    if ($browserIcons[$browserName]) {

    $browserIcon = ($icon) ? '<img src="' . $browserIcons[$browserName] . '" border="0">&nbsp;' : $browserIcon;

    }

    return $browserIcon . $browserName;

    }

    }

    }

    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    $browser_with_icon = defineUserAgent($userAgent); // Browser name with a icon

    $browser_without_icon = defineUserAgent($userAgent, false); // Browser name without a icon

    echo 'You are running <b>' . $browser_with_icon . '</b><br /><br />';

    echo 'You are running <b>' . $browser_without_icon . '</b>';

    ?>
    Hope this helps you all.
  2. Hayze New Member

    Member Since:
    Sep 6, 2007
    Message Count:
    949
    Likes Received:
    0
    Occupation:
    Un Occupied.
    Location:
    To your left... no your other left.
    Wow, nice post. Couldn't you also use call letters to find out if they are using a certain browser, then do something different.
  3. iMatt New Member

    Member Since:
    Nov 11, 2007
    Message Count:
    50
    Likes Received:
    0
    Occupation:
    Student
    Location:
    Manchester, UK
    Yes, it is possible to edit the code to instead of showing images, to show letters or numbers.
Thread Status:
Not open for further replies.

Share This Page