Geekpedia Programming Tutorials






Detect operating system from user agent string

This code uses an array of user strings and their matching operating systems so that it can detect what operatings system the user is using.

On Saturday, November 3rd 2007 at 02:02 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.6 with 11 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. $OSList = array
  2. (
  3.         // Match user agent string with operating systems
  4.         'Windows 3.11' => 'Win16',
  5.         'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
  6.         'Windows 98' => '(Windows 98)|(Win98)',
  7.         'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
  8.         'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
  9.         'Windows Server 2003' => '(Windows NT 5.2)',
  10.         'Windows Vista' => '(Windows NT 6.0)',
  11.         'Windows 7' => '(Windows NT 7.0)',
  12.         'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
  13.         'Windows ME' => 'Windows ME',
  14.         'Open BSD' => 'OpenBSD',
  15.         'Sun OS' => 'SunOS',
  16.         'Linux' => '(Linux)|(X11)',
  17.         'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
  18.         'QNX' => 'QNX',
  19.         'BeOS' => 'BeOS',
  20.         'OS/2' => 'OS/2',
  21.         'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
  22. );
  23.  
  24. // Loop through the array of user agents and matching operating systems
  25. foreach($OSList as $CurrOS=>$Match)
  26. {
  27.         // Find a match
  28.         if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
  29.         {
  30.                 // We found the correct match
  31.                 break;
  32.         }
  33. }
  34. // You are using Windows Vista
  35. echo "You are using ".$CurrOS;
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by jack on Monday, June 9th 2008 at 04:16 AM

http://www.infysolutions.com

by josh on Monday, June 9th 2008 at 04:17 AM

<a href="http://www.infysolutions.com">infysolutions</a>

by STOP on Sunday, February 1st 2009 at 02:16 AM

STOP spamming us, idiots!

by Tom on Wednesday, April 15th 2009 at 10:36 AM

Line 11 should be

'Windows 7' => '(Windows NT 6.1)',



Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons