Detect OS Family
Hi,
I'm in need to get OS name on an app I'm developing. This script is excelent but it give too much details for my needs. What I present here is a new parameter which only tells the family of the OS on the client (windows,linux,unix,mac). The modification is simple and is specifically concentred on the which_os function. You could get a zipped php from this link: <link removed, please post code or patches, not the entire file, that's now how you provide patches, sorry> To use this new parameter, simple use :: Code ::
$osFam = browser_detection("os_family") And $osFam should contains one value of windows,linux,unix or mac. Hope this serve some one. Pep Back to top |
|||||
Please post the patched sections of the code here, don't link to a download site that doesn't even show the code, ok?
I'm removing that link until we get a real code sample posted here. Back to top |
|||||
Didn't read the post guides... sorry
Following those lines are the changes I've made.
Simply I've added the variable $os_family which is returned on the switch. In turn, this variable is filled in the which_os function in the same way the other components of the result array are setted. As I told by contact message, it is not fully tested. :: Code ::
function browser_detection( $which_test ) { ... if ( !$b_repeat ) { //initialize all variables with default values to prevent error $dom_browser = false; $type = 'bot';// default to bot since you never know with bots $safe_browser = false; $os = ''; $os_number = ''; $os_family = ""; ... // get os data, mac os x test requires browser/version information, this is a change from older scripts $a_os_data = which_os( $browser_user_agent, $browser_name, $version_number ); $os = $a_os_data[0];// os name, abbreviated $os_number = $a_os_data[1];// os number or version if available $os_family = $a_os_data[2]; // os family ... switch ( $which_test ) { ... case 'os_family': return $os_family; break; default: break; } function which_os ( $browser_string, $browser_name, $version_number ) { // initialize variables $os = ''; $os_version = ''; $os_family = ''; /* packs the os array use this order since some navigator user agents will put 'macintosh' in the navigator user agent string which would make the nt test register true */ $a_mac = array( 'mac68k', 'macppc' );// this is not used currently // same logic, check in order to catch the os's in order, last is always default item $a_unix = array( 'freebsd', 'openbsd', 'netbsd', 'bsd', 'unixware', 'solaris', 'sunos', 'sun4', 'sun5', 'suni86', 'sun', 'irix5', 'irix6', 'irix', 'hpux9', 'hpux10', 'hpux11', 'hpux', 'hp-ux', 'aix1', 'aix2', 'aix3', 'aix4', 'aix5', 'aix', 'sco', 'unixware', 'mpras', 'reliant', 'dec', 'sinix', 'unix' ); // only sometimes will you get a linux distro to id itself... $a_linux = array( 'ubuntu', 'kubuntu', 'xubuntu', 'mepis', 'xandros', 'linspire', 'winspire', 'sidux', 'kanotix', 'debian', 'opensuse', 'suse', 'fedora', 'redhat', 'slackware', 'slax', 'mandrake', 'mandriva', 'gentoo', 'sabayon', 'linux' ); $a_linux_process = array ( 'i386', 'i586', 'i686' );// not use currently // note, order of os very important in os array, you will get failed ids if changed $a_os = array( 'beos', 'os2', 'amiga', 'webtv', 'mac', 'nt', 'win', $a_unix, $a_linux ); //os tester $i_count = count( $a_os ); for ( $i = 0; $i < $i_count; $i++ ) { // unpacks os array, assigns to variable $s_os = $a_os[$i]; // assign os to global os variable, os flag true on success // !stristr($browser_string, "linux" ) corrects a linux detection bug if ( !is_array( $s_os ) && stristr( $browser_string, $s_os ) && !stristr( $browser_string, "linux" ) ) { $os = $s_os; switch ( $os ) { case 'win': if ( strstr( $browser_string, '95' ) ) { $os_version = '95'; $os_family = 'windows'; } elseif ( ( strstr( $browser_string, '9x 4.9' ) ) || ( strstr( $browser_string, 'me' ) ) ) { $os_version = 'me'; $os_family = 'windows'; } elseif ( strstr( $browser_string, '98' ) ) { $os_version = '98'; $os_family = 'windows'; } elseif ( strstr( $browser_string, '2000' ) )// windows 2000, for opera ID { $os_version = 5.0; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'xp' ) )// windows 2000, for opera ID { $os_version = 5.1; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, '2003' ) )// windows server 2003, for opera ID { $os_version = 5.2; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'vista' ) )// windows vista, for opera ID { $os_version = 6.0; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'ce' ) )// windows CE { $os_version = 'ce'; $os_family = 'windows'; } break; case 'nt': if ( strstr( $browser_string, 'nt 6.1' ) )// windows 7 { $os_version = 6.1; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 6.0' ) )// windows vista/server 2008 { $os_version = 6.0; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 5.2' ) )// windows server 2003 { $os_version = 5.2; $os = 'nt'; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 5.1' ) || strstr( $browser_string, 'xp' ) )// windows xp { $os_version = 5.1;// $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 5' ) || strstr( $browser_string, '2000' ) )// windows 2000 { $os_version = 5.0; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 4' ) )// nt 4 { $os_version = 4; $os_family = 'windows'; } elseif ( strstr( $browser_string, 'nt 3' ) )// nt 4 { $os_version = 3; $os_family = 'windows'; } break; case 'mac': if ( strstr( $browser_string, 'os x' ) ) { $os_version = 10; $os_family = 'mac'; } //this is a crude test for os x, since safari, camino, ie 5.2, & moz >= rv 1.3 //are only made for os x elseif ( ( $browser_name == 'saf' ) || ( $browser_name == 'cam' ) || ( ( $browser_name == 'moz' ) && ( $version_number >= 1.3 ) ) || ( ( $browser_name == 'ie' ) && ( $version_number >= 5.2 ) ) ) { $os_version = 10; } break; default: break; } break; } // check that it's an array, check it's the second to last item //in the main os array, the unix one that is elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 2 ) ) ) { $i_count = count($s_os); for ($j = 0; $j < $i_count; $j++) { if ( stristr( $browser_string, $s_os[$j] ) ) { $os = 'unix'; //if the os is in the unix array, it's unix, obviously... $os_version = ( $s_os[$j] != 'unix' ) ? $s_os[$j] : '';// assign sub unix version from the unix array $os_family = 'unix'; break; } } } // check that it's an array, check it's the last item //in the main os array, the linux one that is elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 1 ) ) ) { $i_count = count($s_os); for ($j = 0; $j < $i_count; $j++) { if ( stristr( $browser_string, $s_os[$j] ) ) { $os = 'lin'; // assign linux distro from the linux array, there's a default //search for 'lin', if it's that, set version to '' $os_version = ( $s_os[$j] != 'linux' ) ? $s_os[$j] : ''; $os_family = 'linux'; break; } } } } // pack the os data array for return to main function $os_data = array( $os, $os_version, $os_family ); return $os_data; } Hope not to forget some fragment of code. Best regards Back to top |
|||||
Thanks, that's about what I was hoping you'd have done, so that will be no problem, that will be added, thanks.
Back to top |
|||||
All times are GMT - 8 Hours
|