SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting PHP to give users browser type

    Is there anyway php can get the users browser type and version??

    KJC
    Eternity

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The server parameter $_SERVER['HTTP_USER_AGENT'] will give you the user agent string, but from there on you'll need to know what you're looking for. There are freely available 'browser sniffer' PHP scripts on a number of PHP dev sites that cut out the guesswork.

    At its simplest, a statement such as:

    Code:
    if((strpos($_SERVER['HTTP_USER_AGENT'],'Mozilla/4') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') === false))
    will check to see if a user is using Netscape 4. I use this line in a PHP-enabled CSS file to deliver simplified styles to Netscape 4 instead of the ones other browsers see.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yer, but the difficult part is when someone has Opera and it's AGENT string returns MSIE

    That trips up a lot of people.... I had a script to single out Opera as well though so far I cannot find it from my CDs

    If I come across it, I'll post it here....

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Serbia
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go, OS & Browser detection:

    PHP Code:
    <?php
    $HTTP_USER_AGENT
    =$_SERVER['HTTP_USER_AGENT'];
    $os=""$osh="";
    if (
    eregi ("(win|microsoft)"$HTTP_USER_AGENT)==true$os="Windows";
    if (
    eregi ("(unix|x11|lynx|konqueror|w3m)"$HTTP_USER_AGENT)==true$os="Unix";
    if (
    eregi ("linux"$HTTP_USER_AGENT)==true$os="Linux";
    if (
    eregi ("bsd"$HTTP_USER_AGENT)==true$os="*BSD";
    if (
    eregi ("sunos"$HTTP_USER_AGENT)==true$os="SunOS";
    if (
    eregi ("hp-ux"$HTTP_USER_AGENT)==true$os="HP-UX";
    if (
    eregi ("os/2"$HTTP_USER_AGENT)==true$os="OS/2";
    if (
    eregi ("qnx"$HTTP_USER_AGENT)==true$os="QNX";
    if (
    eregi ("mac"$HTTP_USER_AGENT)==true$os="Macintosh";
    if (
    eregi ("(powerpc|ppc)(mac)"$HTTP_USER_AGENT)==true$os="Macintosh PowerPC";
    if (
    eregi ("beos"$HTTP_USER_AGENT)==true$os="Beos";
    if (
    eregi ("solaris"$HTTP_USER_AGENT)==true$os="Solaris";
    if (
    eregi ("amigaos"$HTTP_USER_AGENT)==true$os="AmigaOS";
    if (
    eregi ("windows nt|winnt"$HTTP_USER_AGENT)==true$os="Windows NT";
    if (
    eregi ("windows nt 4"$HTTP_USER_AGENT)==true$os="Windows NT 4";
    if (
    eregi ("(windows nt 5|windows 2000)"$HTTP_USER_AGENT)==true$os="Windows 2000";
    if (
    eregi ("(windows nt 5.1|windows xp)"$HTTP_USER_AGENT)==true$os="Windows XP";
    if (
    eregi ("windows me"$HTTP_USER_AGENT)==true$os="Windows ME";
    if (
    eregi ("(windows 98|win98)"$HTTP_USER_AGENT)==true$os="Windows 98";
    if (
    eregi ("(windows 95|win95)"$HTTP_USER_AGENT)==true$os="Windows 95";
    if (
    eregi ("(windows 3.1|win3.1)"$HTTP_USER_AGENT)==true$os="Windows 3.1";
    if (
    eregi ("(windows 3.11|win3.11)"$HTTP_USER_AGENT)==true$os="Windows 3.11";
    if (
    eregi ("(mandrake|mdk)"$HTTP_USER_AGENT)==true$os="Linux Mandrake";
    if (
    eregi ("debian"$HTTP_USER_AGENT)==true$os="Linux Debian";
    if (
    eregi ("webtv"$HTTP_USER_AGENT)==true$os="WebTV";

    $browser=""$browserh="";
    if (
    eregi ("(netscape|mozilla)"$HTTP_USER_AGENT)==true$browser="Netscape";
    if (
    eregi ("mozilla/3"$HTTP_USER_AGENT)==true$browser="Netscape 3";
    if (
    eregi ("mozilla/4.5"$HTTP_USER_AGENT)==true$browser="Netscape 4.5";
    if (
    eregi ("mozilla/4.6"$HTTP_USER_AGENT)==true$browser="Netscape 4.6";
    if (
    eregi ("mozilla/4.7"$HTTP_USER_AGENT)==true$browser="Netscape 4.7";
    if (
    eregi ("(mozilla/5|gecko/)"$HTTP_USER_AGENT)==true$browser="Mozilla 1";
    if (
    eregi ("netscape/6"$HTTP_USER_AGENT)==true$browser="Netscape 6";
    if (
    eregi ("msie"$HTTP_USER_AGENT)==true$browser="IE";
    if (
    eregi ("msie 3"$HTTP_USER_AGENT)==true$browser="IE 3";
    if (
    eregi ("msie 3.0"$HTTP_USER_AGENT)==true$browser="IE 3.0";
    if (
    eregi ("msie 3.01"$HTTP_USER_AGENT)==true$browser="IE 3.01";
    if (
    eregi ("msie 4"$HTTP_USER_AGENT)==true$browser="IE 4";
    if (
    eregi ("msie 4.0"$HTTP_USER_AGENT)==true$browser="IE 4.0";
    if (
    eregi ("msie 4.01"$HTTP_USER_AGENT)==true$browser="IE 4.01";
    if (
    eregi ("msie 5"$HTTP_USER_AGENT)==true$browser="IE 5";
    if (
    eregi ("msie 5.0"$HTTP_USER_AGENT)==true$browser="IE 5.0";
    if (
    eregi ("msie 5.01"$HTTP_USER_AGENT)==true$browser="IE 5.01";
    if (
    eregi ("msie 5.1"$HTTP_USER_AGENT)==true$browser="IE 5.1";
    if (
    eregi ("msie 5.5"$HTTP_USER_AGENT)==true$browser="IE 5.5";
    if (
    eregi ("msie 6"$HTTP_USER_AGENT)==true$browser="IE 6";
    if (
    eregi ("msie 6.0"$HTTP_USER_AGENT)==true$browser="IE 6.0";
    if (
    eregi ("msie 6.0b"$HTTP_USER_AGENT)==true$browser="IE 6.0b";
    if (
    eregi ("opera"$HTTP_USER_AGENT)==true$browser="Opera";
    if (
    eregi ("opera.2"$HTTP_USER_AGENT)==true$browser="Opera 2";
    if (
    eregi ("opera.3"$HTTP_USER_AGENT)==true$browser="Opera 3";
    if (
    eregi ("opera.4"$HTTP_USER_AGENT)==true$browser="Opera 4";
    if (
    eregi ("opera.5"$HTTP_USER_AGENT)==true$browser="Opera 5";
    if (
    eregi ("opera.5.11"$HTTP_USER_AGENT)==true$browser="Opera 5.11";
    if (
    eregi ("opera.5.12"$HTTP_USER_AGENT)==true$browser="Opera 5.12";
    if (
    eregi ("opera.6"$HTTP_USER_AGENT)==true$browser="Opera 6";
    if (
    eregi ("opera.7.01"$HTTP_USER_AGENT)==true$browser="Opera 7.01";
    if (
    eregi ("opera.7.10"$HTTP_USER_AGENT)==true$browser="Opera 7.10";
    if (
    eregi ("opera.7.11"$HTTP_USER_AGENT)==true$browser="Opera 7.11";
    if (
    eregi ("lynx"$HTTP_USER_AGENT)==true$browser="lynx";
    if (
    eregi ("w3m"$HTTP_USER_AGENT)==true$browser="w3m";
    if (
    eregi ("konqueror"$HTTP_USER_AGENT)==true$browser="Konqueror";
    ?>
    Save it as a separate file, include it where you want, and use $os and $browser vars as you wish.
    Wondering about best weight loss pill? Take a look at Proactol.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers!

    Thats great mate
    Eternity

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ereg functions = slow...

    PHP Code:
    <?php
    /**************************************************
    * User Agent Detector
    * -------------------

    * Deluxe Portal Version 2.0
    *
    * This SINGLE FILE is Licensed under the LGPL which
    * can be found at
    * [url=http://www.gnu.org/licenses/lgpl.txt]http://www.gnu.org/licenses/lgpl.txt[/url]
    **************************************************/
    $_REMOTE = array();
    $_REMOTE['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
    $_REMOTE['BROWSER']['TYPE'] = 0;
    $_REMOTE['BROWSER']['VERSION'] = 0;
    $_REMOTE['BROWSER']['GECKO'] = 0;
    $_REMOTE['OS']['PLATFORM'] = 0;
    $_REMOTE['OS']['TYPE'] = 0;
    $_REMOTE['OS']['VERSION'] = 0;
      
    if (
    strpos($_REMOTE['USER_AGENT'], 'Opera') === 0)
    {
     
    $_REMOTE['BROWSER']['TYPE'] = 'Opera';
     list(
    $version$dummy) = explode(' '$_REMOTE['USER_AGENT'], 2);
     list(
    $dummy$version) = explode('/'$version2);
     if (
    strpos($version'v') === 0$version dp_substr($version1);
    }
    elseif (
    strpos($_REMOTE['USER_AGENT'], 'Mozilla') === 0)
    {
     if (
    strpos($_REMOTE['USER_AGENT'], 'Opera'))
     {
      
    $_REMOTE['BROWSER']['TYPE'] = 'Opera';
           list(
    $dummy$version) = explode('\)'$_REMOTE['USER_AGENT'], 2);
           list(
    $dummy$dummy$version) = explode(' '$version);
           if (
    strpos($version'v') === 0$version dp_substr($version1);
     }
     elseif (
    strpos($_REMOTE['USER_AGENT'], '(compatible'))
     {
      if (
    strpos($_REMOTE['USER_AGENT'], 'Konqueror'))
      {
       
    $_REMOTE['BROWSER']['TYPE'] = 'Konqueror';
             list(
    $dummy$version) = explode('Konqueror/'$_REMOTE['USER_AGENT'], 2);
             list(
    $version$dummy) = explode(';'$version2);
         }
      elseif (
    strpos($_REMOTE['USER_AGENT'], 'MSIE'))
      {
             
    $_REMOTE['BROWSER']['TYPE'] = 'Internet Explorer';
             list(
    $dummy$version) = explode('MSIE'$_REMOTE['USER_AGENT'], 2);
             list(
    $version$dummy) = explode(';'$version2);
            }
     }
     else
     {
         if (
    strpos($_REMOTE['USER_AGENT'], 'Netscape6'))
      {
          
    $_REMOTE['BROWSER']['TYPE'] = 'Netscape';
          list(
    $dummy$version) = explode('Netscape6/'$_REMOTE['USER_AGENT'], 2);
         }
      elseif (
    strpos($_REMOTE['USER_AGENT'], 'Gecko'))
      {
          
    $_REMOTE['BROWSER']['TYPE'] = 'Mozilla';
          list(
    $dummy$version) = explode('rv:'$_REMOTE['USER_AGENT'], 2);
          list(
    $version$dummy) = explode('\)'$version2);
      }
      elseif (
    strpos($_REMOTE['USER_AGENT'], 'Sun'))
      {
       
    $_REMOTE['BROWSER']['TYPE'] = 'Hot Java';
       list(
    $version$dummy) = explode(' '$_REMOTE['USER_AGENT'], 2);
       list(
    $dummy$version) = explode('/'$version2);
         }
      else
      {
          
    $_REMOTE['BROWSER']['TYPE'] = 'Netscape';
             list(
    $version$dummy) = explode(' '$_REMOTE['USER_AGENT'], 2);
             list(
    $dummy$version) = explode('/'$version2);
            }
     }
    }
    elseif (
    strpos($_REMOTE['USER_AGENT'], 'amaya') === 0)
    {
     
    $_REMOTE['BROWSER']['TYPE'] = 'Amaya';
     list(
    $dummy$version) = explode('amaya/'$_REMOTE['USER_AGENT'], 2);
     list(
    $version$dummy) = explode(' '$version2);
    }
    elseif (
    strpos($_REMOTE['USER_AGENT'], 'EPOC') === 0)
    {
     
    $_REMOTE['BROWSER']['TYPE'] = 'EPOC Web';
     list(
    $dummy$version) = explode('WTL/'$_REMOTE['USER_AGENT'], 2);
     list(
    $version$dummy) = explode(' '$version2);
    }
    if (isset(
    $version)) $_REMOTE['BROWSER']['VERSION'] = trim($version);
    if (
    preg_match('/gecko\/([0-9]+)/i'$_REMOTE['USER_AGENT'], $match)) $_REMOTE['BROWSER']['GECKO'] = $match[1];
    $_REMOTE['OS']['PLATFORM'] = '*NIX';
    if (
    preg_match_all('/(win[dows]*)[\s]?([0-9a-z]*)[\w\s]?([a-z0-9.]*)/i'$_REMOTE['USER_AGENT'], $match))
    {
     
    $v  $match[2][count($match[0])-1];
     
    $v2 $match[3][count($match[0])-1];
     
    $_REMOTE['OS']['PLATFORM'] = 'Win32';
     
    $_REMOTE['OS']['VERSION'] = $v2;
     if (
    stristr($v,'NT'))
     {
      if (
    $v2 == '5.0')
       
    $v 'Windows 2000';
      elseif (
    $v2 == '5.1')
       
    $v 'Windows XP';
      elseif (
    $v2 == '5.2')
       
    $v 'Windows 2003';
      elseif (
    $v2 == '6.0')
       
    $v 'Windows Codename Longhorn';
      else
       
    $v 'Windows NT';
     }
     elseif (
    stristr($v,'9x'))
      
    $v 'Windows 9x';
     elseif (
    $v.$v2 == '16bit')
     {
      
    $v 'Windows';
      
    $_REMOTE['OS']['PLATFORM'] = 'Win16';
     }
     else
      
    $v .= $v2;
     
     if (empty(
    $v)) $v 'win';
     
    $_REMOTE['OS']['TYPE'] = $v;
    }
    elseif( 
    preg_match('/os\/2|ibm-webexplorer/i'$_REMOTE['USER_AGENT']))
    {
     
    $_REMOTE['OS']['TYPE'] = 'OS/2';
        
    $_REMOTE['OS']['PLATFORM'] = 'OS/2';
    }
    elseif (
    preg_match('/(68)[k0]{1,3}|[p\S]{1,5}(pc)/i'$_REMOTE['USER_AGENT'], $match))
    {
     
    $_REMOTE['OS']['TYPE'] = 'Mac OS';
     
    $platform = !empty($match[1]) ? '68k' '';
     
    $platform = !empty($match[2]) ? 'Power PC' $platform;
     
    $_REMOTE['OS']['PLATFORM'] = $platform;
    }
    elseif (
    preg_match('/(sun|i86)[os\s]*([0-9]*)/i',$_REMOTE['USER_AGENT'],$match))
    {
     if(!
    stristr('sun',$match[1]))
     {
      
    $match[1] = 'sun'.$match[1];
      
    $_REMOTE['OS']['TYPE'] = $match[1];
      
    $_REMOTE['OS']['VERSION'] = $match[2];
     }
     elseif (
    preg_match('/(irix)[\s]*([0-9]*)/i'$_REMOTE['USER_AGENT'], $match))
     {
      
    $_REMOTE['OS']['TYPE'] = $match[1];
      
    $_REMOTE['OS']['VERSION'] = $match[2];
     }
     elseif (
    preg_match('/(hp-ux)[\s]*([0-9]*)/i'$_REMOTE['USER_AGENT'], $match))
     {
      
    $_REMOTE['OS']['TYPE'] = 'HP-UNIX';
      
    $_REMOTE['OS']['VERSION'] = (int) $match[2];
        }
     elseif (
    preg_match('/aix([0-9]*)/i'$_REMOTE['USER_AGENT'], $match))
     {
      
    $_REMOTE['OS']['TYPE'] = 'AIX';
      
    $_REMOTE['OS']['VERSION'] = $match[1];
     }
     elseif (
    preg_match('/dec|osfl|alphaserver|ultrix|alphastation/i'$_REMOTE['USER_AGENT'], $match))
      
    $_REMOTE['OS']['TYPE'] = 'DEC';
     elseif (
    preg_match('/vax|openvms/i'$_REMOTE['USER_AGENT'], $match))
      
    $_REMOTE['OS']['TYPE'] = 'VMS';
        elseif (
    preg_match('/sco|unix_sv/i'$_REMOTE['USER_AGENT'], $match))
      
    $_REMOTE['OS']['TYPE'] = 'SCO';
     elseif (
    stristr('unix_system_v'$_REMOTE['USER_AGENT']))
      
    $_REMOTE['OS']['TYPE'] = 'UNIXWARE';
     elseif (
    stristr('ncr'$_REMOTE['USER_AGENT']))
      
    $_REMOTE['OS']['TYPE'] = 'MPRAS';
     elseif (
    stristr('reliantunix'$_REMOTE['USER_AGENT']))
      
    $_REMOTE['OS']['TYPE'] = 'Reliant';
     elseif(
    stristr('sinix'$_REMOTE['USER_AGENT']))
      
    $_REMOTE['OS']['TYPE'] = 'SINIX';
        elseif (
    preg_match('/(free)?(bsd)/i'$_REMOTE['USER_AGENT'], $match))
      
    $_REMOTE['OS']['TYPE'] = $match[1].$match[2];
     elseif(
    preg_match('/x11|inux/i'$_REMOTE['USER_AGENT'], $match))
      
    $_REMOTE['OS']['TYPE'] = 'Linux';
     elseif (
    stristr('epoc'$_REMOTE['USER_AGENT']))
     {
      
    $_REMOTE['OS']['TYPE'] = 'EPOC';
      
    $_REMOTE['OS']['PLATFORM'] = 'EPOC';
     }
     else
      
    $_REMOTE['OS']['PLATFORM'] = 'UNKNOWN';
    }
    ?>
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow... All that script just to detect a browser ?

    I suppose it'll cover all eventuallities though... the script I had was about 20 lines and done basically the same though w/out all the trimmings.



Bookmarks

Posting Permissions

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