Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.4 with 26 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

infysolutions

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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM

The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',

by cluke009 on Sunday, November 29th 2009 at 06:20 PM

Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx

by Ali on Monday, June 14th 2010 at 06:41 AM

thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html

by Ali on Monday, June 14th 2010 at 06:42 AM

thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html

by Dima on Tuesday, October 5th 2010 at 05:27 AM

Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.

by Dima on Tuesday, October 5th 2010 at 05:35 AM

HA ha I am wrong! I know nothing in php :)

by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM

eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6

by sam on Tuesday, October 19th 2010 at 08:38 AM

thanks

by sam on Tuesday, October 19th 2010 at 08:38 AM

thanks

by Justin on Friday, October 29th 2010 at 10:44 AM

Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}

by Joe on Wednesday, November 17th 2010 at 05:59 PM

What should $agent be as argument?
example:

echo os_info(???);

thx

by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM

echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.

by ala on Wednesday, February 9th 2011 at 10:06 PM

but you didn't detect server 2008

by Kevin on Friday, February 11th 2011 at 04:29 PM

RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()

by Kevin F. on Friday, February 11th 2011 at 05:57 PM

Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by rtfg on Tuesday, March 22nd 2011 at 10:02 AM






TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',
'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
foreach($oses as $os=>$pattern)
if (preg_match('/'.$pattern.'/i', $agent))
return $os;
return 'Unknown';
}
by Joe on Wednesday, November 17th 2010 at 05:59 PM
What should $agent be as argument?
example:

echo os_info(???);

thx
by George I. Tsopouridis on Friday, January 7th 2011 at 07:13 AM
echo os_info($_SERVER['HTTP_USER_AGENT']);
It worked for me.
by ala on Wednesday, February 9th 2011 at 10:06 PM
but you didn't detect server 2008
by Kevin on Friday, February 11th 2011 at 04:29 PM
RE: JOE

"What should $agent be..."

$agent isn't necessary in this function as $agent is declared within the function..

I would suggest shortening the function to
function os_info()
by Kevin F. on Friday, February 11th 2011 at 05:57 PM
Okay. The above samples did not work well..

So I rewrote the function loosely based on the above examples. I also replaced the depreciated eregi with preg_match..

This works:


public function os() {
//

// method to detect customer OS
// BROWSER::os()

//

$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

return $os;


}


note: my class is BROWSER so when I call this function I do so with: echo BROWSER::os();

If you would like to not use a function or don't understand how they work. Just do this:
$osList = array
(
'Windows 7' => 'windows nt 6.1',
'Windows Vista' => 'windows nt 6.0',
'Windows Server 2003' => 'windows nt 5.2',
'Windows XP' => 'windows nt 5.1',
'Windows 2000 sp1' => 'windows nt 5.01',
'Windows 2000' => 'windows nt 5.0',
'Windows NT 4.0' => 'windows nt 4.0',
'Windows Me' => 'win 9x 4.9',
'Windows 98' => 'windows 98',
'Windows 95' => 'windows 95',
'Windows CE' => 'windows ce',
'Windows (version unknown)' => 'windows',
'OpenBSD' => 'openbsd',
'SunOS' => 'sunos',
'Ubuntu' => 'ubuntu',
'Linux' => '(linux)|(x11)',
'Mac OSX Beta (Kodiak)' => 'mac os x beta',
'Mac OSX Cheetah' => 'mac os x 10.0',
'Mac OSX Puma' => 'mac os x 10.1',
'Mac OSX Jaguar' => 'mac os x 10.2',
'Mac OSX Panther' => 'mac os x 10.3',
'Mac OSX Tiger' => 'mac os x 10.4',
'Mac OSX Leopard' => 'mac os x 10.5',
'Mac OSX Snow Leopard' => 'mac os x 10.6',
'Mac OSX Lion' => 'mac os x 10.7',
'Mac OSX (version unknown)' => 'mac os x',
'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',
'QNX' => 'QNX',
'BeOS' => 'beos',
'OS2' => 'os/2',
'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'
);

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = strtolower($useragent);

foreach($osList as $os=>$match) {
if (preg_match('/' . $match . '/i', $useragent)) {
break;
} else {
$os = "Not automatically detected.$useragent";
}
}

echo $os;

Comment on this tutorial
Name: Email:
Message:
Subscribe me to future replies

Related Source Code
There is no related code.

Related Tutorials
There are no related tutorials.

PHP Job Search
My skills include:

Enter a City:

Select a State:



Advanced Search >>
Sponsors


Discover Geekpedia
News
Tutorials
Source Code
Pictures
Videos
Reviews
Special Reports
Bargains
Resources
Icons
Newsgroups
Knowledge Base
Books
Magazines
Downloads
Jobs
Forums
Search
Other Resources
driver update program
Solution to errors
Newegg Coupon Codes

About Geekpedia
CONTACT US
ADVERTISE
STATISTICS
FAQ
Legal
PRIVACY POLICY
TERMS AND CONDITIONS
COPYRIGHT INFORMATION
More Resources
EXTERNAL RESOURCES
FREE BOOKS
FREE MAGAZINES
TECH JOBS
DOWNLOADS
SEARCH
Closed Sections
ANNOUNCEMENTS
LEXICON
REVIEWS
HARDWARE
PROJECTS
POLLS
Geekpedia Network
ERRORBANK.COM
WINDOWSVIENNA.COM
DOTNETONWEB.COM

Copyright © Geekpedia 2004 - 2011. All Rights Reserved.
Web Hosting by Intersynq in New York.







TUTORIALSCODEHOW TONEWSFORUMSPICTURESVIDEOSICONSNEWSGROUPS
Building a C# Chat Client and Server
A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
in C# Programming Tutorials
Getting Hard Drive Information
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
in C# Programming Tutorials
UPS Shipping Calculator
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
in PHP Programming Tutorials
Create Your Own Rich Text Editor
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials
Search

Tutorials
PROGRAMMING TUTORIALS
ActionScript (5)
ASP (7)
ASP.NET (28)
Assembly (1)
C (6)
C# (132)
C (33)
CSS (6)
General (4)
HTML (6)
Java (28)
JavaScript (38)
MySQL (2)
Objective C (1)
Pascal (1)
PHP (61)
Python (3)
Ruby (18)
Silverlight (1)
SQL (8)
VB.NET (5)
Visual Basic (3)
IT Jobs
FROM CAREERBUILDER
IT Jobs
VP/General Manager, Software
Concepts NREC
US-VT-White River Junction
Technical Recruiter
Sapphire Technologies U. S.
US-MN-Mineapolis
Top Talent Needed! Center Tech Support Jobs!
Telvista
US-TX-Dallas
Interaction Designer - Visual Designer
Duo Consulting
US-IL-Chicago
Crystal Reports Developer
Technisource
US-NY-New York
Data Services Technician
SofterWare, Inc.
US-PA-Horsham
Service Desk Analyst/Specialist
Citadel Federal Credit Union
US-PA-Exton
MFS – SQL Server Database (DBA) Professional

US-MN-Mendota Heights
ITIL Process Architect
Cargill
US-MN-Minneapolis
Business Development Manager (Sales)
Global Employment Solutions - IT
US-PA-Bala Cynwyd

Post a Resume
Post a Job
See more jobs at
CareerBuilder.com


Geekpedia » PHP Code » Detect operating system from user agent string
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.3 with 22 votes)
Contextual Ads


More PHP Resources
PHP Tutorials
PHP Code
PHP Jobs
Advertisement

$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList

Rate this code snippet

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
infysolutions
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)',


by cluke009 on Sunday, November 29th 2009 at 06:16 PM
The windows me detection is off a bit.

The fix below should do the trick

'windows-me' => '(Windows 98)|(Win 9x 4.90)',
'windows-98' => '(Windows 98)|(Win98)',
by cluke009 on Sunday, November 29th 2009 at 06:20 PM
Edit for the code above. This should cover all bases.

'Windows ME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',

More info at: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
by Ali on Monday, June 14th 2010 at 06:41 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Ali on Monday, June 14th 2010 at 06:42 AM
thanks :) , it worked

and this is an arabized version of the function:
http://me.the-ghost.com/2010/06/blog-post_14.html
by Dima on Tuesday, October 5th 2010 at 05:27 AM
Hello.
You can do vice versa
$OSList = array
(
'Win16' => 'Windows 3.11',
'Windows 95' => 'Windows 95',
'Win95' => 'Windows 95',
'Windows_95' => 'Windows 95',
'Windows 98' => 'Windows 98',
'Win98'=> => 'Windows 98'
);

$CurrOS = $OSList[$_SERVER['HTTP_USER_AGENT'];
echo "You are using ".$CurrOS;

the definition of $OSList looks silly, but all the calculations do php itself without eregi masterclass.
by Dima on Tuesday, October 5th 2010 at 05:35 AM
HA ha I am wrong! I know nothing in php :)
by MyShadowSelf.com on Wednesday, October 6th 2010 at 02:53 PM
eregi() is depreciated now, best to use preg_match() or it'll stop working when your ost updates to PHP6
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by sam on Tuesday, October 19th 2010 at 08:38 AM
thanks
by Justin on Friday, October 29th 2010 at 10:44 AM
Here is an updated version of Andrew's script using preg_match and the user_agent changes thanks to cluke009 and Tom.

function os_info($agent) {
// the order of this array is important
$oses = array(
'Win311' => 'Win16',
'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
'Win98' => '(Windows 98)|(Win98)',
'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
'WinXP' => '(Windows NT 5.1)|(Windows XP)',
'WinServer2003' => '(Windows NT 5.2)',
'WinVista' => '(Windows NT 6.0)',
'Win7' => '(Windows NT 6.1)',
'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'OpenBSD' => 'OpenBSD',
'SunOS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS2' => 'OS/2',

by credit card application on Saturday, December 10th 2011 at 08:24 AM

I have been tinkering with Ajax for quite a while. I think I would try your suggestions here to get better results. and pls i wiil like to learn more on php like the code for how to make payment through alert pay and also Good tutorial,but how we can get the checked string using javascript. f I change my Master Volume, my Wave volume should also change to the same value. This must run in the background.

by testing on Saturday, December 10th 2011 at 11:34 AM

http://www.webdesignera.com/

by loan on Monday, January 16th 2012 at 06:06 AM

Don't you acknowledge that it's correct time to get the credit loans, which can realize your dreams.

by Casper on Saturday, January 28th 2012 at 09:43 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by problem solving activities on Sunday, January 29th 2012 at 03:48 PM


I love this article, it's very well

by Free RingtonesFor Virgin Mobile on Tuesday, January 31st 2012 at 07:09 PM

Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.

by casino on Saturday, February 4th 2012 at 06:49 AM

I need to say that you are doing a fantastic job. Please keep up the great work.

by Start a Cleaning Business on Saturday, February 4th 2012 at 08:46 PM

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this.

by Bodybuilding Produkte on Wednesday, February 15th 2012 at 04:36 AM

thanks to the support of modern browsers; this tutorial will walk you through it.
in JavaScript Programming Tutorials

by counter strike 1.6 download on Thursday, February 16th 2012 at 02:22 PM

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand.

by steam showers on Tuesday, May 1st 2012 at 12:49 PM

They are made in a thoughtful and interesting manner.

by brickmasterj on Wednesday, May 2nd 2012 at 01:57 AM

I've made a list with even more OSes:
List is a work-in-progress, so it will be updated.
If you found a mistake, please let me know: (brickmasterj@minewars.co.uk)!

(
// Mircrosoft Windows Operating Systems
'Windows 3.11' => '(Win16)',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows 2000 Service Pack 1' => '(Windows NT 5.01)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
'Windows 8' => '(Windows NT 6.2)|(Windows 8)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => '(Windows ME)|(Windows 98; Win 9x 4.90 )',
'Windows CE' => '(Windows CE)',
// UNIX Like Operating Systems
'Mac OS X Kodiak (beta)' => '(Mac OS X beta)',
'Mac OS X Cheetah' => '(Mac OS X 10.0)',
'Mac OS X Puma' => '(Mac OS X 10.1)',
'Mac OS X Jaguar' => '(Mac OS X 10.2)',
'Mac OS X Panther' => '(Mac OS X 10.3)',
'Mac OS X Tiger' => '(Mac OS X 10.4)',
'Mac OS X Leopard' => '(Mac OS X 10.5)',
'Mac OS X Snow Leopard' => '(Mac OS X 10.6)',
'Mac OS X Lion' => '(Mac OS X 10.7)',
'Mac OS X' => '(Mac OS X)',
'Mac OS' => '(Mac_PowerPC)|(PowerPC)|(Macintosh)',
'Open BSD' => '(OpenBSD)',
'SunOS' => '(SunOS)',
'Solaris 11' => '(Solaris/11)|(Solaris11)',
'Solaris 10' => '((Solaris/10)|(Solaris10))',
'Solaris 9' => '((Solaris/9)|(Solaris9))',
'CentOS' => '(CentOS)',
'QNX' => '(QNX)',
// Kernels
'UNIX' => '(UNIX)',
// Linux Operating Systems
'Ubuntu 12.10' => '(Ubuntu/12.10)|(Ubuntu 12.10)',
'Ubuntu 12.04 LTS' => '(Ubuntu/12.04)|(Ubuntu 12.04)',
'Ubuntu 11.10' => '(Ubuntu/11.10)|(Ubuntu 11.10)',
'Ubuntu 11.04' => '(Ubuntu/11.04)|(Ubuntu 11.04)',
'Ubuntu 10.10' => '(Ubuntu/10.10)|(Ubuntu 10.10)',
'Ubuntu 10.04 LTS' => '(Ubuntu/10.04)|(Ubuntu 10.04)',
'Ubuntu 9.10' => '(Ubuntu/9.10)|(Ubuntu 9.10)',
'Ubuntu 9.04' => '(Ubuntu/9.04)|(Ubuntu 9.04)',
'Ubuntu 8.10' => '(Ubuntu/8.10)|(Ubuntu 8.10)',
'Ubuntu 8.04 LTS' => '(Ubuntu/8.04)|(Ubuntu 8.04)',
'Ubuntu 6.06 LTS' => '(Ubuntu/6.06)|(Ubuntu 6.06)',
'Red Hat Linux' => '(Red Hat)',
'Red Hat Enterprise Linux' => '(Red Hat Enterprise)',
'Fedora 17' => '(Fedora/17)|(Fedora 17)',
'Fedora 16' => '(Fedora/16)|(Fedora 16)',
'Fedora 15' => '(Fedora/15)|(Fedora 15)',
'Fedora 14' => '(Fedora/14)|(Fedora 14)',
'Chromium OS' => '(ChromiumOS)',
'Google Chrome OS' => '(ChromeOS)',
// Kernel
'Linux' => '(Linux)|(X11)',
// BSD Operating Systems
'OpenBSD' => '(OpenBSD)',
'FreeBSD' => '(FreeBSD)',
'NetBSD' => '(NetBSD)',
// Mobile Devices
'Andriod' => '(Android)',
'iPod' => '(iPod)',
'iPhone' => '(iPhone)',
'iPad' => '(iPad)',
//DEC Operating Systems
'OS/8' => '(OS/8)|(OS8)',
'Older DEC OS' => '(DEC)|(RSTS)|(RSTS/E)',
'WPS-8' => '(WPS-8)|(WPS8)',
// BeOS Like Operating Systems
'BeOS' => '(BeOS)|(BeOS r5)',
'BeIA' => '(BeIA)',
// OS/2 Operating Systems
'OS/2 2.0' => '(OS/220)|(OS/2 2.0)',
'OS/2' => '(OS/2)|(OS2)',
// Search engines
'Search engine or robot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(msnbot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

by brickmasterj on Wednesday, May 2nd 2012 at 01:57 AM

I've made a list with even more OSes:
List is a work-in-progress, so it will be updated.
If you found a mistake, please let me know: (brickmasterj@minewars.co.uk)!

(
// Mircrosoft Windows Operating Systems
'Windows 3.11' => '(Win16)',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows 2000 Service Pack 1' => '(Windows NT 5.01)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
'Windows 8' => '(Windows NT 6.2)|(Windows 8)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => '(Windows ME)|(Windows 98; Win 9x 4.90 )',
'Windows CE' => '(Windows CE)',
// UNIX Like Operating Systems
'Mac OS X Kodiak (beta)' => '(Mac OS X beta)',
'Mac OS X Cheetah' => '(Mac OS X 10.0)',
'Mac OS X Puma' => '(Mac OS X 10.1)',
'Mac OS X Jaguar' => '(Mac OS X 10.2)',
'Mac OS X Panther' => '(Mac OS X 10.3)',
'Mac OS X Tiger' => '(Mac OS X 10.4)',
'Mac OS X Leopard' => '(Mac OS X 10.5)',
'Mac OS X Snow Leopard' => '(Mac OS X 10.6)',
'Mac OS X Lion' => '(Mac OS X 10.7)',
'Mac OS X' => '(Mac OS X)',
'Mac OS' => '(Mac_PowerPC)|(PowerPC)|(Macintosh)',
'Open BSD' => '(OpenBSD)',
'SunOS' => '(SunOS)',
'Solaris 11' => '(Solaris/11)|(Solaris11)',
'Solaris 10' => '((Solaris/10)|(Solaris10))',
'Solaris 9' => '((Solaris/9)|(Solaris9))',
'CentOS' => '(CentOS)',
'QNX' => '(QNX)',
// Kernels
'UNIX' => '(UNIX)',
// Linux Operating Systems
'Ubuntu 12.10' => '(Ubuntu/12.10)|(Ubuntu 12.10)',
'Ubuntu 12.04 LTS' => '(Ubuntu/12.04)|(Ubuntu 12.04)',
'Ubuntu 11.10' => '(Ubuntu/11.10)|(Ubuntu 11.10)',
'Ubuntu 11.04' => '(Ubuntu/11.04)|(Ubuntu 11.04)',
'Ubuntu 10.10' => '(Ubuntu/10.10)|(Ubuntu 10.10)',
'Ubuntu 10.04 LTS' => '(Ubuntu/10.04)|(Ubuntu 10.04)',
'Ubuntu 9.10' => '(Ubuntu/9.10)|(Ubuntu 9.10)',
'Ubuntu 9.04' => '(Ubuntu/9.04)|(Ubuntu 9.04)',
'Ubuntu 8.10' => '(Ubuntu/8.10)|(Ubuntu 8.10)',
'Ubuntu 8.04 LTS' => '(Ubuntu/8.04)|(Ubuntu 8.04)',
'Ubuntu 6.06 LTS' => '(Ubuntu/6.06)|(Ubuntu 6.06)',
'Red Hat Linux' => '(Red Hat)',
'Red Hat Enterprise Linux' => '(Red Hat Enterprise)',
'Fedora 17' => '(Fedora/17)|(Fedora 17)',
'Fedora 16' => '(Fedora/16)|(Fedora 16)',
'Fedora 15' => '(Fedora/15)|(Fedora 15)',
'Fedora 14' => '(Fedora/14)|(Fedora 14)',
'Chromium OS' => '(ChromiumOS)',
'Google Chrome OS' => '(ChromeOS)',
// Kernel
'Linux' => '(Linux)|(X11)',
// BSD Operating Systems
'OpenBSD' => '(OpenBSD)',
'FreeBSD' => '(FreeBSD)',
'NetBSD' => '(NetBSD)',
// Mobile Devices
'Andriod' => '(Android)',
'iPod' => '(iPod)',
'iPhone' => '(iPhone)',
'iPad' => '(iPad)',
//DEC Operating Systems
'OS/8' => '(OS/8)|(OS8)',
'Older DEC OS' => '(DEC)|(RSTS)|(RSTS/E)',
'WPS-8' => '(WPS-8)|(WPS8)',
// BeOS Like Operating Systems
'BeOS' => '(BeOS)|(BeOS r5)',
'BeIA' => '(BeIA)',
// OS/2 Operating Systems
'OS/2 2.0' => '(OS/220)|(OS/2 2.0)',
'OS/2' => '(OS/2)|(OS2)',
// Search engines
'Search engine or robot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(msnbot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

by EuroConsult Dubai on Wednesday, May 16th 2012 at 07:43 AM

i didn't want to know what to do with them.I should give your link to my neighbour, he's a computer geek. I loved that chaos formed from letters, signs and digits but i didn't want to know what to do with them.I should give your link to my neighbour, he's a computer geek.

by byman64 on Saturday, May 26th 2012 at 08:50 AM

I used OS/2 and was nice from "brickmasterj" remember it!

Just a question, concerning the iPhone / IPad maybe it's better detect also the version like for windows x.x

In my case $_SERVER['HTTP_USER_AGENT'] shows:

iPAD : OS_5_1
Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3


iPhone : OS_5_1_1
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3

I think it's useful is someone publish the full function to help who needs it.

If it is not a problem I can publish it on my site refereing this great discussion. Can I?

Any idea how fix it ?

I use php but I am not and advanced phph developer.
Please, can you send me the right and full function ...

by byman64 on Saturday, May 26th 2012 at 08:50 AM

I used OS/2 and was nice from "brickmasterj" remember it!

Just a question, concerning the iPhone / IPad maybe it's better detect also the version like for windows x.x

In my case $_SERVER['HTTP_USER_AGENT'] shows:

iPAD : OS_5_1
Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3


iPhone : OS_5_1_1
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3

I think it's useful is someone publish the full function to help who needs it.

If it is not a problem I can publish it on my site refereing this great discussion. Can I?

Any idea how fix it ?

I use php but I am not and advanced phph developer.
Please, can you send me the right and full function ...

by Pussy.com on Tuesday, May 29th 2012 at 06:49 PM

nice one i like it very much please keep posting thank you .

by Dark_coder on Friday, June 15th 2012 at 12:31 PM

Thanks guys !! i was searshing for this script !!

by Dark_coder on Friday, June 15th 2012 at 12:31 PM

Thanks guys !! i was searching for this script !!

by Dark_coder on Friday, June 15th 2012 at 12:31 PM

Thanks guys !! i was searching for this script !!

by Dark_coder on Friday, June 15th 2012 at 12:32 PM

Thanks guys !! i was searching for this script !!

by on Monday, August 6th 2012 at 02:05 AM

I loved that chaos formed from letters, signs and digits but i didn't want to know what to do with them.

by Gold Bars for Sale on Wednesday, August 29th 2012 at 04:44 PM

i didn't want to know what to do with them.I should give your link to my neighbour, he's a computer geek. I loved that chaos formed from letters, signs and digits but i didn't want to know what to do with them.I should give your link to my neighbour, he's a computer geek.

by Best Acne Treatments on Thursday, September 6th 2012 at 07:33 AM

You have really interesting blog, keep up posting such informative posts!

by Best Acne Treatment on Thursday, September 6th 2012 at 08:03 AM

You have really interesting blog, keep up posting such informative posts!

by Acne Scar Treatment on Thursday, September 6th 2012 at 08:22 AM

You have really interesting blog, keep up posting such informative posts!

by Hemocyl on Thursday, September 6th 2012 at 10:16 AM

You have really interesting blog, keep up posting such informative posts!

by Teeth Whitening Reviews on Thursday, September 6th 2012 at 10:56 AM

You have really interesting blog, keep up posting such informative posts!

by Diet Pills on Thursday, September 6th 2012 at 11:23 AM

You have really interesting blog, keep up posting such informative posts!

by Best Diet Pills on Thursday, September 6th 2012 at 01:05 PM

You have really interesting blog, keep up posting such informative posts!

by powdered milk biscuits on Thursday, November 1st 2012 at 06:03 AM

I should give your link to my neighbour, he's a computer geek. I loved that chaos formed from letters, signs and digits but i

by invisible fence company rochester ny on Tuesday, November 6th 2012 at 04:03 AM

he's a computer geek. I loved that chaos formed from letters, signs and digits but i

by kamran on Wednesday, December 19th 2012 at 12:15 PM

Thanks guys !! i was searching for this script ! http://lemon-benefits.com/Articles/Benefits-of-Lemon-Water.html

by kamran on Wednesday, December 19th 2012 at 12:17 PM

Thanks guys !! i was searching for this script ! http://lemon-benefits.com/Articles/Benefits-of-Lemon-Water.html

by Margahayuland on Sunday, June 9th 2013 at 07:45 AM

Loop through the array of user agents and matching operating systems
http://sooboos.com/margahayuland/margahayuland-42-tahun-membangun-ruang-hidup.php
http://sooboos.com/situs-berita/penting-panas-perlu-dan-seruu.php
http://sooboos.com
http://wadahpromoindonesia.blogspot.com/2013/05/margahayuland-42-tahun-membangun.html
http://tech-kinodeon.blogspot.com/2013/02/agen-bola-ligabet88-promo-bonus-100.html
http://tech-kinodeon.blogspot.com/2013/04/penting-panas-perlu-dan-seruu.html
http://tech-kinodeon.blogspot.com/2013/05/korek-api-gas-fighter-indonesia.html


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 >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources