function browser_detection($which_test, $test_excludes = '', $external_ua_string = '') { /* uncomment the global variable declaration if you want the variables to be available on a global level throughout your php page, make sure that php is configured to support the use of globals first! Use of globals should be avoided however, and they are not necessary with this script /* /* global $a_full_assoc_data, $a_mobile_data, $a_moz_data, $a_webkit_data, $b_dom_browser, $b_repeat, $b_safe_browser, $browser_name, $browser_number, $browser_math_number, $browser_user_agent, $browser_working, $ie_version, $mobile_test, $moz_number, $moz_rv, $moz_rv_full, $moz_release_date, $moz_type, $os_number, $os_type, $true_ie_number, $ua_type, $webkit_type, $webkit_type_number; */ script_time(); // set script timer to start timing static $a_full_assoc_data, $a_mobile_data, $a_moz_data, $a_webkit_data, $b_dom_browser, $b_repeat, $b_safe_browser, $browser_name, $browser_number, $browser_math_number, $browser_user_agent, $browser_working, $ie_version, $mobile_test, $moz_number, $moz_rv, $moz_rv_full, $moz_release_date, $moz_type, $os_number, $os_type, $true_ie_number, $ua_type, $webkit_type, $webkit_type_number; // switch off the optimization for external ua string testing. if ($external_ua_string) { $b_repeat = false; } /* this makes the test only run once no matter how many times you call it since all the variables are filled on the first run through, it's only a matter of returning the the right ones */ if (!$b_repeat) { //initialize all variables with default values to prevent error $a_browser_math_number = ''; $a_full_assoc_data = ''; $a_full_data = ''; $a_mobile_data = ''; $a_moz_data = ''; $a_os_data = ''; $a_unhandled_browser = ''; $a_webkit_data = ''; $b_dom_browser = false; $b_os_test = true; $b_mobile_test = true; $b_safe_browser = false; $b_success = false; // boolean for if browser found in main test $browser_math_number = ''; $browser_temp = ''; $browser_working = ''; $browser_number = ''; $ie_version = ''; $mobile_test = ''; $moz_release_date = ''; $moz_rv = ''; $moz_rv_full = ''; $moz_type = ''; $moz_number = ''; $os_number = ''; $os_type = ''; $run_time = ''; $true_ie_number = ''; $ua_type = 'bot'; // default to bot since you never know with bots $webkit_type = ''; $webkit_type_number = ''; // set the excludes if required if ($test_excludes) { switch ($test_excludes) { case '1': $b_os_test = false; break; case '2': $b_mobile_test = false; break; case '3': $b_os_test = false; $b_mobile_test = false; break; default: die('Error: bad $test_excludes parameter 2 used: ' . $test_excludes); break; } } /* make navigator user agent string lower case to make sure all versions get caught isset protects against blank user agent failure. tolower also lets the script use strstr instead of stristr, which drops overhead slightly. */ if ($external_ua_string) { $browser_user_agent = strtolower($external_ua_string); } elseif (isset($_SERVER['HTTP_USER_AGENT'])) { $browser_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']); } else { $browser_user_agent = ''; } /* pack the browser type array, in this order the order is important, because opera must be tested first, then omniweb [which has safari data in string], same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string. Note that $b_dom_browser is set for all modern dom browsers, this gives you a default to use. array[0] = id string for useragent, array[1] is if dom capable, array[2] is working name for browser, array[3] identifies navigator useragent type Note: all browser strings are in lower case to match the strtolower output, this avoids possible detection errors Note: These are the navigator user agent types: bro - modern, css supporting browser. bbro - basic browser, text only, table only, defective css implementation bot - search type spider dow - known download agent lib - standard http libraries mobile - handheld or mobile browser, set using $mobile_test */ // known browsers, list will be updated routinely, check back now and then $a_browser_types = array(array('opera', true, 'op', 'bro'), array('msie', true, 'ie', 'bro'), array('webkit', true, 'webkit', 'bro'), array('konqueror', true, 'konq', 'bro'), array('gecko', true, 'moz', 'bro'), array('netpositive', false, 'netp', 'bbro'), array('lynx', false, 'lynx', 'bbro'), array('elinks ', false, 'elinks', 'bbro'), array('elinks', false, 'elinks', 'bbro'), array('links2', false, 'links2', 'bbro'), array('links ', false, 'links', 'bbro'), array('links', false, 'links', 'bbro'), array('w3m', false, 'w3m', 'bbro'), array('webtv', false, 'webtv', 'bbro'), array('amaya', false, 'amaya', 'bbro'), array('dillo', false, 'dillo', 'bbro'), array('ibrowse', false, 'ibrowse', 'bbro'), array('icab', false, 'icab', 'bro'), array('crazy browser', true, 'ie', 'bro'), array('bingbot', false, 'bing', 'bot'), array('exabot', false, 'exabot', 'bot'), array('googlebot', false, 'google', 'bot'), array('google web preview', false, 'googlewp', 'bot'), array('mediapartners-google', false, 'adsense', 'bot'), array('yahoo-verticalcrawler', false, 'yahoo', 'bot'), array('yahoo! slurp', false, 'yahoo', 'bot'), array('yahoo-mm', false, 'yahoomm', 'bot'), array('inktomi', false, 'inktomi', 'bot'), array('slurp', false, 'inktomi', 'bot'), array('fast-webcrawler', false, 'fast', 'bot'), array('msnbot', false, 'msn', 'bot'), array('ask jeeves', false, 'ask', 'bot'), array('teoma', false, 'ask', 'bot'), array('scooter', false, 'scooter', 'bot'), array('openbot', false, 'openbot', 'bot'), array('ia_archiver', false, 'ia_archiver', 'bot'), array('zyborg', false, 'looksmart', 'bot'), array('almaden', false, 'ibm', 'bot'), array('baiduspider', false, 'baidu', 'bot'), array('psbot', false, 'psbot', 'bot'), array('gigabot', false, 'gigabot', 'bot'), array('naverbot', false, 'naverbot', 'bot'), array('surveybot', false, 'surveybot', 'bot'), array('boitho.com-dc', false, 'boitho', 'bot'), array('objectssearch', false, 'objectsearch', 'bot'), array('answerbus', false, 'answerbus', 'bot'), array('sohu-search', false, 'sohu', 'bot'), array('iltrovatore-setaccio', false, 'il-set', 'bot'), array('w3c_validator', false, 'w3c', 'lib'), array('wdg_validator', false, 'wdg', 'lib'), array('libwww-perl', false, 'libwww-perl', 'lib'), array('jakarta commons-httpclient', false, 'jakarta', 'lib'), array('python-urllib', false, 'python-urllib', 'lib'), array('getright', false, 'getright', 'dow'), array('wget', false, 'wget', 'dow'), array('mozilla/4.', false, 'ns', 'bbro'), array('mozilla/3.', false, 'ns', 'bbro'), array('mozilla/2.', false, 'ns', 'bbro')); //array( '', false ); // browser array template /* moz types array note the order, netscape6 must come before netscape, which is how netscape 7 id's itself. rv comes last in case it is plain old mozilla. firefox/netscape/seamonkey need to be later Thanks to: http://www.zytrax.com/tech/web/firefox-history.html */ $a_moz_types = array('bonecho', 'camino', 'epiphany', 'firebird', 'flock', 'galeon', 'iceape', 'icecat', 'k-meleon', 'minimo', 'multizilla', 'phoenix', 'songbird', 'swiftfox', 'seamonkey', 'shadowfox', 'shiretoko', 'iceweasel', 'firefox', 'minefield', 'netscape6', 'netscape', 'rv'); /* webkit types, this is going to expand over time as webkit browsers spread konqueror is probably going to move to webkit, so this is preparing for that It will now default to khtml. gtklauncher is the temp id for epiphany, might change. Defaults to applewebkit, and will all show the webkit number. */ $a_webkit_types = array('arora', 'chrome', 'epiphany', 'gtklauncher', 'icab', 'konqueror', 'maxthon', 'midori', 'omniweb', 'rekonq', 'safari', 'shiira', 'uzbl', 'applewebkit', 'webkit'); /* run through the browser_types array, break if you hit a match, if no match, assume old browser or non dom browser, assigns false value to $b_success. */ $i_count = count($a_browser_types); for ($i = 0; $i < $i_count; $i++) { //unpacks browser array, assigns to variables, need to not assign til found in string $browser_temp = $a_browser_types[$i][0]; // text string to id browser from array if (strstr($browser_user_agent, $browser_temp)) { /* it defaults to true, will become false below if needed this keeps it easier to keep track of what is safe, only explicit false assignment will make it false. */ $b_safe_browser = true; $browser_name = $browser_temp; // text string to id browser from array // assign values based on match of user agent string $b_dom_browser = $a_browser_types[$i][1]; // hardcoded dom support from array $browser_working = $a_browser_types[$i][2]; // working name for browser $ua_type = $a_browser_types[$i][3]; // sets whether bot or browser switch ($browser_working) { // this is modified quite a bit, now will return proper netscape version number // check your implementation to make sure it works case 'ns': $b_safe_browser = false; $browser_number = get_item_version($browser_user_agent, 'mozilla'); break; case 'moz': /* note: The 'rv' test is not absolute since the rv number is very different on different versions, for example Galean doesn't use the same rv version as Mozilla, neither do later Netscapes, like 7.x. For more on this, read the full mozilla numbering conventions here: http://www.mozilla.org/releases/cvstags.html */ // this will return alpha and beta version numbers, if present $moz_rv_full = get_item_version($browser_user_agent, 'rv'); // this slices them back off for math comparisons $moz_rv = substr($moz_rv_full, 0, 3); // this is to pull out specific mozilla versions, firebird, netscape etc.. $j_count = count($a_moz_types); for ($j = 0; $j < $j_count; $j++) { if (strstr($browser_user_agent, $a_moz_types[$j])) { $moz_type = $a_moz_types[$j]; $moz_number = get_item_version($browser_user_agent, $moz_type); break; } } /* this is necesary to protect against false id'ed moz'es and new moz'es. this corrects for galeon, or any other moz browser without an rv number */ if (!$moz_rv) { // you can use this if you are running php >= 4.2 if (function_exists('floatval')) { $moz_rv = floatval($moz_number); } else { $moz_rv = substr($moz_number, 0, 3); } $moz_rv_full = $moz_number; } // this corrects the version name in case it went to the default 'rv' for the test if ($moz_type == 'rv') { $moz_type = 'mozilla'; } //the moz version will be taken from the rv number, see notes above for rv problems $browser_number = $moz_rv; // gets the actual release date, necessary if you need to do functionality tests get_set_count('set', 0); $moz_release_date = get_item_version($browser_user_agent, 'gecko/'); /* Test for mozilla 0.9.x / netscape 6.x test your javascript/CSS to see if it works in these mozilla releases, if it does, just default it to: $b_safe_browser = true; */ if ($moz_release_date < 20020400 || $moz_rv < 1) { $b_safe_browser = false; } break; case 'ie': /* note we're adding in the trident/ search to return only first instance in case of msie 8, and we're triggering the break last condition in the test, as well as the test for a second search string, trident/ Sample: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0) */ $browser_number = get_item_version($browser_user_agent, $browser_name, true, 'trident/'); // construct the proper real number if it's in compat mode and msie 10 if (strstr($browser_number, '7.')) { if (strstr($browser_user_agent, 'trident/7')) { // note that 7.0 becomes 11 when adding 4, but if it's 7.1 it will be 11.1 $true_ie_number = $browser_number + 4; } elseif (strstr($browser_user_agent, 'trident/6')) { // note that 7.0 becomes 10 when adding 3, but if it's 7.1 it will be 10.1 $true_ie_number = $browser_number + 3; } elseif (strstr($browser_user_agent, 'trident/5')) { // note that 7.0 becomes 9 when adding 2, but if it's 7.1 it will be 9.1 $true_ie_number = $browser_number + 2; } elseif (strstr($browser_user_agent, 'trident/4')) { // note that 7.0 becomes 8 when adding 1, but if it's 7.1 it will be 8.1 $true_ie_number = $browser_number + 1; } } // the 9 series is finally standards compatible, html 5 etc, so worth a new id if ($browser_number >= 9) { $ie_version = 'ie9x'; } elseif ($browser_number >= 7) { $ie_version = 'ie7x'; } elseif (strstr($browser_user_agent, 'mac')) { $ie_version = 'ieMac'; } elseif ($browser_number >= 5) { $ie_version = 'ie5x'; } elseif ($browser_number > 3 && $browser_number < 5) { $b_dom_browser = false; $ie_version = 'ie4'; // this depends on what you're using the script for, make sure this fits your needs $b_safe_browser = true; } else { $ie_version = 'old'; $b_dom_browser = false; $b_safe_browser = false; } break; case 'op': $browser_number = get_item_version($browser_user_agent, $browser_name); // opera is leaving version at 9.80 (or xx) for 10.x - see this for explanation // http://dev.opera.com/articles/view/opera-ua-string-changes/ if (strstr($browser_number, '9.') && strstr($browser_user_agent, 'version/')) { get_set_count('set', 0); $browser_number = get_item_version($browser_user_agent, 'version/'); } if ($browser_number < 5) { $b_safe_browser = false; } break; /* note: webkit returns always the webkit version number, not the specific user agent version, ie, webkit 583, not chrome 0.3 */ /* note: webkit returns always the webkit version number, not the specific user agent version, ie, webkit 583, not chrome 0.3 */ case 'webkit': // note that this is the Webkit version number $browser_number = get_item_version($browser_user_agent, $browser_name); // this is to pull out specific webkit versions, safari, google-chrome etc.. $j_count = count($a_webkit_types); for ($j = 0; $j < $j_count; $j++) { if (strstr($browser_user_agent, $a_webkit_types[$j])) { $webkit_type = $a_webkit_types[$j]; /* and this is the webkit type version number, like: chrome 1.2 if omni web, we want the count 2, not default 1 */ if ($webkit_type == 'omniweb') { get_set_count('set', 2); } $webkit_type_number = get_item_version($browser_user_agent, $webkit_type); // epiphany hack if ($a_webkit_types[$j] == 'gtklauncher') { $browser_name = 'epiphany'; } else { $browser_name = $a_webkit_types[$j]; } break; } } break; default: $browser_number = get_item_version($browser_user_agent, $browser_name); break; } // the browser was id'ed $b_success = true; break; } } //assigns defaults if the browser was not found in the loop test if (!$b_success) { /* this will return the first part of the browser string if the above id's failed usually the first part of the browser string has the navigator useragent name/version in it. This will usually correctly id the browser and the browser number if it didn't get caught by the above routine. If you want a '' to do a if browser == '' type test, just comment out all lines below except for the last line, and uncomment the last line. If you want undefined values, the browser_name is '', you can always test for that */ // delete this part if you want an unknown browser returned $browser_name = substr($browser_user_agent, 0, strcspn($browser_user_agent, '();')); // this extracts just the browser name from the string, if something usable was found if ($browser_name && preg_match('/[^0-9][a-z]*-*\\ *[a-z]*\\ *[a-z]*/', $browser_name, $a_unhandled_browser)) { $browser_name = $a_unhandled_browser[0]; if ($browser_name == 'blackberry') { get_set_count('set', 0); } $browser_number = get_item_version($browser_user_agent, $browser_name); } else { $browser_name = 'NA'; $browser_number = 'NA'; } // then uncomment this part //$browser_name = '';//deletes the last array item in case the browser was not a match } // get os data, mac os x test requires browser/version information, this is a change from older scripts if ($b_os_test) { $a_os_data = get_os_data($browser_user_agent, $browser_working, $browser_number); $os_type = $a_os_data[0]; // os name, abbreviated $os_number = $a_os_data[1]; // os number or version if available } /* this ends the run through once if clause, set the boolean to true so the function won't retest everything */ $b_repeat = true; /* pulls out primary version number from more complex string, like 7.5a, use this for numeric version comparison */ if ($browser_number && preg_match('/[0-9]*\\.*[0-9]*/', $browser_number, $a_browser_math_number)) { $browser_math_number = $a_browser_math_number[0]; //print_r($a_browser_math_number); } if ($b_mobile_test) { $mobile_test = check_is_mobile($browser_user_agent); if ($mobile_test) { $a_mobile_data = get_mobile_data($browser_user_agent); $ua_type = 'mobile'; } } } //$browser_number = $_SERVER["REMOTE_ADDR"]; /* This is where you return values based on what parameter you used to call the function $which_test is the passed parameter in the initial browser_detection('os') for example returns the os version only. Update deprecated parameter names to new names */ switch ($which_test) { case 'math_number': $which_test = 'browser_math_number'; break; case 'number': $which_test = 'browser_number'; break; case 'browser': $which_test = 'browser_working'; break; case 'moz_version': $which_test = 'moz_data'; break; case 'true_msie_version': $which_test = 'true_ie_number'; break; case 'type': $which_test = 'ua_type'; break; case 'webkit_version': $which_test = 'webkit_data'; break; } /* assemble these first so they can be included in full return data, using static variables Note that there's no need to keep repacking these every time the script is called */ if (!$a_moz_data) { $a_moz_data = array($moz_type, $moz_number, $moz_rv, $moz_rv_full, $moz_release_date); } if (!$a_webkit_data) { $a_webkit_data = array($webkit_type, $webkit_type_number, $browser_number); } $run_time = script_time(); // then pack the primary data array if (!$a_full_assoc_data) { $a_full_assoc_data = array('browser_working' => $browser_working, 'browser_number' => $browser_number, 'ie_version' => $ie_version, 'dom' => $b_dom_browser, 'safe' => $b_safe_browser, 'os' => $os_type, 'os_number' => $os_number, 'browser_name' => $browser_name, 'ua_type' => $ua_type, 'browser_math_number' => $browser_math_number, 'moz_data' => $a_moz_data, 'webkit_data' => $a_webkit_data, 'mobile_test' => $mobile_test, 'mobile_data' => $a_mobile_data, 'true_ie_number' => $true_ie_number, 'run_time' => $run_time); } // return parameters, either full data arrays, or by associative array index key switch ($which_test) { // returns all relevant browser information in an array with standard numberic indexes case 'full': $a_full_data = array($browser_working, $browser_number, $ie_version, $b_dom_browser, $b_safe_browser, $os_type, $os_number, $browser_name, $ua_type, $browser_math_number, $a_moz_data, $a_webkit_data, $mobile_test, $a_mobile_data, $true_ie_number, $run_time); // print_r( $a_full_data ); return $a_full_data; break; // returns all relevant browser information in an associative array // returns all relevant browser information in an associative array case 'full_assoc': return $a_full_assoc_data; break; default: # check to see if the data is available, otherwise it's user typo of unsupported option if (isset($a_full_assoc_data[$which_test])) { return $a_full_assoc_data[$which_test]; } else { die("You passed the browser detector an unsupported option for parameter 1: " . $which_test); } break; } }
function browser_detection($which_test) { /* uncomment the global variable declaration if you want the variables to be available on a global level throughout your php page, make sure that php is configured to support the use of globals first! Use of globals should be avoided however, and they are not necessary with this script */ /* global $dom_browser, $safe_browser, $browser_user_agent, $browser_name, $s_browser, $ie_version, $browser_version_number, $mobile_test, $a_mobile_data, $os_number, $os_type, $b_repeat, $moz_type, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release_date, $math_version_number, $ua_type, $webkit_type, $webkit_type_number; */ static $dom_browser, $safe_browser, $browser_user_agent, $browser_name, $s_browser, $ie_version, $browser_version_number, $mobile_test, $a_mobile_data, $os_number, $os_type, $b_repeat, $moz_type, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release_date, $math_version_number, $ua_type, $webkit_type, $webkit_type_number; /* this makes the test only run once no matter how many times you call it since all the variables are filled on the first run through, it's only a matter of returning the the right ones */ if (!$b_repeat) { //initialize all variables with default values to prevent error $dom_browser = false; $ua_type = 'bot'; // default to bot since you never know with bots $safe_browser = false; $a_os_data = ''; $os_number = ''; $os_type = ''; $browser_name = ''; $browser_version_number = ''; $math_version_number = ''; $ie_version = ''; $mobile_test = ''; $a_mobile_data = ''; $a_moz_data = ''; $moz_type = ''; $moz_version_number = ''; $moz_rv = ''; $moz_rv_full = ''; $moz_release_date = ''; $a_webkit_data = ''; $webkit_type = ''; $webkit_type_number = ''; $b_success = false; // boolean for if browser found in main test //make navigator user agent string lower case to make sure all versions get caught // isset protects against blank user agent failure $browser_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : ''; /* pack the browser type array, in this order the order is important, because opera must be tested first, then omniweb [which has safari data in string], same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string. note that $dom_browser is set for all modern dom browsers, this gives you a default to use. array[0] = id string for useragent, array[1] is if dom capable, array[2] is working name for browser, array[3] identifies navigator useragent type Note: all browser strings are in lower case to match the strtolower output, this avoids possible detection errors Note: These are the navigator user agent types: bro - modern, css supporting browser. bbro - basic browser, text only, table only, defective css implementation bot - search type spider dow - known download agent lib - standard http libraries mobile - handheld or mobile browser, set using $mobile_test */ // known browsers, list will be updated routinely, check back now and then $a_browser_types = array(array('opera', true, 'op', 'bro'), array('msie', true, 'ie', 'bro'), array('webkit', true, 'webkit', 'bro'), array('konqueror', true, 'konq', 'bro'), array('gecko', true, 'moz', 'bro'), array('netpositive', false, 'netp', 'bbro'), array('lynx', false, 'lynx', 'bbro'), array('elinks ', false, 'elinks', 'bbro'), array('elinks', false, 'elinks', 'bbro'), array('links2', false, 'links2', 'bbro'), array('links ', false, 'links', 'bbro'), array('links', false, 'links', 'bbro'), array('w3m', false, 'w3m', 'bbro'), array('webtv', false, 'webtv', 'bbro'), array('amaya', false, 'amaya', 'bbro'), array('dillo', false, 'dillo', 'bbro'), array('ibrowse', false, 'ibrowse', 'bbro'), array('icab', false, 'icab', 'bro'), array('crazy browser', true, 'ie', 'bro'), array('googlebot', false, 'google', 'bot'), array('mediapartners-google', false, 'adsense', 'bot'), array('yahoo-verticalcrawler', false, 'yahoo', 'bot'), array('yahoo! slurp', false, 'yahoo', 'bot'), array('yahoo-mm', false, 'yahoomm', 'bot'), array('inktomi', false, 'inktomi', 'bot'), array('slurp', false, 'inktomi', 'bot'), array('fast-webcrawler', false, 'fast', 'bot'), array('msnbot', false, 'msn', 'bot'), array('ask jeeves', false, 'ask', 'bot'), array('teoma', false, 'ask', 'bot'), array('scooter', false, 'scooter', 'bot'), array('openbot', false, 'openbot', 'bot'), array('ia_archiver', false, 'ia_archiver', 'bot'), array('zyborg', false, 'looksmart', 'bot'), array('almaden', false, 'ibm', 'bot'), array('baiduspider', false, 'baidu', 'bot'), array('psbot', false, 'psbot', 'bot'), array('gigabot', false, 'gigabot', 'bot'), array('naverbot', false, 'naverbot', 'bot'), array('surveybot', false, 'surveybot', 'bot'), array('boitho.com-dc', false, 'boitho', 'bot'), array('objectssearch', false, 'objectsearch', 'bot'), array('answerbus', false, 'answerbus', 'bot'), array('sohu-search', false, 'sohu', 'bot'), array('iltrovatore-setaccio', false, 'il-set', 'bot'), array('w3c_validator', false, 'w3c', 'lib'), array('wdg_validator', false, 'wdg', 'lib'), array('libwww-perl', false, 'libwww-perl', 'lib'), array('jakarta commons-httpclient', false, 'jakarta', 'lib'), array('python-urllib', false, 'python-urllib', 'lib'), array('getright', false, 'getright', 'dow'), array('wget', false, 'wget', 'dow'), array('mozilla/4.', false, 'ns', 'bbro'), array('mozilla/3.', false, 'ns', 'bbro'), array('mozilla/2.', false, 'ns', 'bbro')); //array( '', false ); // browser array template /* moz types array note the order, netscape6 must come before netscape, which is how netscape 7 id's itself. rv comes last in case it is plain old mozilla. firefox/netscape/seamonkey need to be later */ $a_moz_types = array('camino', 'epiphany', 'firebird', 'flock', 'galeon', 'k-meleon', 'minimo', 'multizilla', 'phoenix', 'swiftfox', 'iceape', 'seamonkey', 'iceweasel', 'firefox', 'netscape6', 'netscape', 'rv'); /* webkit types, this is going to expand over time as webkit browsers spread konqueror is probably going to move to webkit, so this is preparing for that It will now default to khtml. gtklauncher is the temp id for epiphany, might change. Defaults to applewebkit, and will all show the webkit number. */ $a_webkit_types = array('arora', 'chrome', 'epiphany', 'gtklauncher', 'konqueror', 'midori', 'omniweb', 'safari', 'applewebkit'); /* run through the browser_types array, break if you hit a match, if no match, assume old browser or non dom browser, assigns false value to $b_success. */ $i_count = count($a_browser_types); for ($i = 0; $i < $i_count; $i++) { //unpacks browser array, assigns to variables $s_browser = $a_browser_types[$i][0]; // text string to id browser from array if (stristr($browser_user_agent, $s_browser)) { // it defaults to true, will become false below if needed // this keeps it easier to keep track of what is safe, only //explicit false assignment will make it false. $safe_browser = true; // assign values based on match of user agent string $dom_browser = $a_browser_types[$i][1]; // hardcoded dom support from array $browser_name = $a_browser_types[$i][2]; // working name for browser $ua_type = $a_browser_types[$i][3]; // sets whether bot or browser switch ($browser_name) { // this is modified quite a bit, now will return proper netscape version number // check your implementation to make sure it works case 'ns': $safe_browser = false; $browser_version_number = get_item_version($browser_user_agent, 'mozilla'); break; case 'moz': /* note: The 'rv' test is not absolute since the rv number is very different on different versions, for example Galean doesn't use the same rv version as Mozilla, neither do later Netscapes, like 7.x. For more on this, read the full mozilla numbering conventions here: http://www.mozilla.org/releases/cvstags.html */ // this will return alpha and beta version numbers, if present $moz_rv_full = get_item_version($browser_user_agent, 'rv'); // this slices them back off for math comparisons $moz_rv = substr($moz_rv_full, 0, 3); // this is to pull out specific mozilla versions, firebird, netscape etc.. $j_count = count($a_moz_types); for ($j = 0; $j < $j_count; $j++) { if (stristr($browser_user_agent, $a_moz_types[$j])) { $moz_type = $a_moz_types[$j]; $moz_version_number = get_item_version($browser_user_agent, $moz_type); break; } } // this is necesary to protect against false id'ed moz'es and new moz'es. // this corrects for galeon, or any other moz browser without an rv number if (!$moz_rv) { // you can use this if you are running php >= 4.2 if (function_exists('floatval')) { $moz_rv = floatval($moz_version_number); } else { $moz_rv = substr($moz_version_number, 0, 3); } $moz_rv_full = $moz_version_number; } // this corrects the version name in case it went to the default 'rv' for the test if ($moz_type == 'rv') { $moz_type = 'mozilla'; } //the moz version will be taken from the rv number, see notes above for rv problems $browser_version_number = $moz_rv; // gets the actual release date, necessary if you need to do functionality tests $moz_release_date = get_item_version($browser_user_agent, 'gecko/'); /* Test for mozilla 0.9.x / netscape 6.x test your javascript/CSS to see if it works in these mozilla releases, if it does, just default it to: $safe_browser = true; */ if ($moz_release_date < 20020400 || $moz_rv < 1) { $safe_browser = false; } break; case 'ie': $browser_version_number = get_item_version($browser_user_agent, $s_browser); // first test for IE 5x mac, that's the most problematic IE out there if (stristr($browser_user_agent, 'mac')) { $ie_version = 'ieMac'; } elseif ($browser_version_number >= 5) { $ie_version = 'ie5x'; } elseif ($browser_version_number > 3 && $browser_version_number < 5) { $dom_browser = false; $ie_version = 'ie4'; // this depends on what you're using the script for, make sure this fits your needs $safe_browser = true; } else { $ie_version = 'old'; $dom_browser = false; $safe_browser = false; } break; case 'op': $browser_version_number = get_item_version($browser_user_agent, $s_browser); if ($browser_version_number < 5) { $safe_browser = false; } break; // note: webkit returns always the webkit version number, not the specific user // agent version, ie, webkit 583, not chrome 0.3 // note: webkit returns always the webkit version number, not the specific user // agent version, ie, webkit 583, not chrome 0.3 case 'webkit': // note that this is the Webkit version number $browser_version_number = get_item_version($browser_user_agent, $s_browser); // this is to pull out specific webkit versions, safari, google-chrome etc.. $j_count = count($a_webkit_types); for ($j = 0; $j < $j_count; $j++) { if (stristr($browser_user_agent, $a_webkit_types[$j])) { $webkit_type = $a_webkit_types[$j]; // and this is the webkit type version number, like: chrome 1.2 $webkit_type_number = get_item_version($browser_user_agent, $webkit_type); // epiphany hack if ($a_webkit_types[$j] == 'gtklauncher') { $s_browser = 'Epiphany'; } else { $s_browser = $a_webkit_types[$j]; } break; } } break; default: $browser_version_number = get_item_version($browser_user_agent, $s_browser); break; } // the browser was id'ed $b_success = true; break; } } //assigns defaults if the browser was not found in the loop test if (!$b_success) { /* this will return the first part of the browser string if the above id's failed usually the first part of the browser string has the navigator useragent name/version in it. This will usually correctly id the browser and the browser number if it didn't get caught by the above routine. If you want a '' to do a if browser == '' type test, just comment out all lines below except for the last line, and uncomment the last line. If you want undefined values, the browser_name is '', you can always test for that */ // delete this part if you want an unknown browser returned $s_browser = substr($browser_user_agent, 0, strcspn($browser_user_agent, '();')); // this extracts just the browser name from the string preg_match('/[^0-9][a-z]*-*\\ *[a-z]*\\ *[a-z]*/', $s_browser, $r); $s_browser = $r[0]; $browser_version_number = get_item_version($browser_user_agent, $s_browser); // then uncomment this part //$s_browser = '';//deletes the last array item in case the browser was not a match } // get os data, mac os x test requires browser/version information, this is a change from older scripts $a_os_data = get_os_data($browser_user_agent, $browser_name, $browser_version_number); $os_type = $a_os_data[0]; // os name, abbreviated $os_number = $a_os_data[1]; // os number or version if available // this ends the run through once if clause, set the boolean //to true so the function won't retest everything $b_repeat = true; // pulls out primary version number from more complex string, like 7.5a, // use this for numeric version comparison $m = array(); if (preg_match('/[0-9]*\\.*[0-9]*/', $browser_version_number, $m)) { $math_version_number = $m[0]; //print_r($m); } $mobile_test = check_is_mobile($browser_user_agent); if ($mobile_test) { $a_mobile_data = get_mobile_data($browser_user_agent); $ua_type = 'mobile'; } } //$browser_version_number = $_SERVER["REMOTE_ADDR"]; /* This is where you return values based on what parameter you used to call the function $which_test is the passed parameter in the initial browser_detection('os') for example call */ // assemble these first so they can be included in full return data $a_moz_data = array($moz_type, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release_date); $a_webkit_data = array($webkit_type, $webkit_type_number, $browser_version_number); switch ($which_test) { case 'safe': // returns true/false if your tests determine it's a safe browser // you can change the tests to determine what is a safeBrowser for your scripts // in this case sub rv 1 Mozillas and Netscape 4x's trigger the unsafe condition return $safe_browser; break; case 'ie_version': // returns ieMac or ie5x return $ie_version; break; case 'moz_version': // returns array of all relevant moz information return $a_moz_data; break; case 'dom': // returns true/fale if a DOM capable browser return $dom_browser; break; case 'os': // returns os name return $os_type; break; case 'os_number': // returns os number if windows return $os_number; break; case 'browser': // returns browser name return $browser_name; break; case 'number': // returns browser number return $browser_version_number; break; case 'full': // returns all relevant browser information in an array $a_full_data = array($browser_name, $browser_version_number, $ie_version, $dom_browser, $safe_browser, $os_type, $os_number, $s_browser, $ua_type, $math_version_number, $a_moz_data, $a_webkit_data, $mobile_test, $a_mobile_data); return $a_full_data; break; case 'type': // returns what type, bot, browser, maybe downloader in future return $ua_type; break; case 'math_number': // returns numerical version number, for number comparisons return $math_version_number; break; case 'webkit_version': // returns array of all relevant webkit information return $a_webkit_data; break; case 'mobile_test': return $mobile_test; break; case 'mobile_data': return $a_mobile_data; break; default: break; } }