Return to Snippet

Revision: 25003
at March 17, 2010 15:01 by frankyfish


Updated Code
function getBrowser(){
       $return = array("navigator"=>"", "platform"=>"pc", "version"=>"", "long_version");
       $browsers = "mozilla msie gecko firefox konqueror safari chrome netscape opera mosaic lynx camino amaya omniweb";
       $browsers = split(" ", $browsers);
       $nua = strToLower($_SERVER['HTTP_USER_AGENT']);

       $l = strlen($nua);
       for ($i=0; $i<count($browsers); $i++){
           $browser = $browsers[$i];
           $n = stristr($nua, $browser);
           if(strlen($n)>0){
               $return["long_version"] = "";
               $return["navigator"] = $browser;
               $j=strpos($nua, $return["navigator"])+$n+strlen($return["navigator"])+1;
               if($return['navigator'] == "safari"){
                   $begining = strpos($nua,"version/")+8;
                   $end = stripos($nua,'safari')-$begining;
                   $return['long_version'] = trim(substr($nua,$begining,$end));
                   $temp = explode(".",$return['long_version']);
                   $return['version'] = $temp[0];
               } else {
                   for (; $j<=$l; $j++){
                       $s = substr ($nua, $j, 1);
                       if(is_numeric($return["long_version"].$s) ) {
                           $return["long_version"] .= $s;
                           $temp = explode(".",$return['long_version']);
                           $return['version'] = $temp[0];
                       } else { break; }
                   }
               }
           }
       }

       if (strpos($nua,"mac") !== false) $return["platform"] = "mac";
       if(strpos($return['long_version'],"/") !== false){
           $return['long_version'] = array_shift(explode("/",$return['long_version']));
       }
       return $return;
}

Revision: 25002
at March 17, 2010 14:35 by frankyfish


Initial Code
function getBrowser(){
	$return = array("nav"=>"", "platform"=>"pc", "ver"=>"");
	$browsers = "mozilla msie gecko firefox konqueror safari netscape navigator opera mosaic lynx camino amaya omniweb";
	$browsers = split(" ", $browsers);
	$nua = strToLower($_SERVER['HTTP_USER_AGENT']);

	$l = strlen($nua);
	for ($i=0; $i<count($browsers); $i++){
		$browser = $browsers[$i];
		$n = stristr($nua, $browser);
		if(strlen($n)>0){
			$return["ver"] = "";
			$return["nav"] = $browser;
			$j=strpos($nua, $return["nav"])+$n+strlen($return["nav"])+1;
			for (; $j<=$l; $j++){
				$s = substr ($nua, $j, 1);
				if(is_numeric($return["ver"].$s) ) {
					$return["ver"] .= $s;
				} else { break; }
			}
		}
	}

	if (strpos($nua,"mac") !== false) $return["platform"] = "mac";
	return $return;
}

Initial URL

                                

Initial Description

                                

Initial Title
PHP Browser Detect

Initial Tags
browser

Initial Language
PHP