PHP Browser Detect


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function getBrowser(){
  2. $return = array("navigator"=>"", "platform"=>"pc", "version"=>"", "long_version");
  3. $browsers = "mozilla msie gecko firefox konqueror safari chrome netscape opera mosaic lynx camino amaya omniweb";
  4. $browsers = split(" ", $browsers);
  5. $nua = strToLower($_SERVER['HTTP_USER_AGENT']);
  6.  
  7. $l = strlen($nua);
  8. for ($i=0; $i<count($browsers); $i++){
  9. $browser = $browsers[$i];
  10. $n = stristr($nua, $browser);
  11. if(strlen($n)>0){
  12. $return["long_version"] = "";
  13. $return["navigator"] = $browser;
  14. $j=strpos($nua, $return["navigator"])+$n+strlen($return["navigator"])+1;
  15. if($return['navigator'] == "safari"){
  16. $begining = strpos($nua,"version/")+8;
  17. $end = stripos($nua,'safari')-$begining;
  18. $return['long_version'] = trim(substr($nua,$begining,$end));
  19. $temp = explode(".",$return['long_version']);
  20. $return['version'] = $temp[0];
  21. } else {
  22. for (; $j<=$l; $j++){
  23. $s = substr ($nua, $j, 1);
  24. if(is_numeric($return["long_version"].$s) ) {
  25. $return["long_version"] .= $s;
  26. $temp = explode(".",$return['long_version']);
  27. $return['version'] = $temp[0];
  28. } else { break; }
  29. }
  30. }
  31. }
  32. }
  33.  
  34. if (strpos($nua,"mac") !== false) $return["platform"] = "mac";
  35. if(strpos($return['long_version'],"/") !== false){
  36. $return['long_version'] = array_shift(explode("/",$return['long_version']));
  37. }
  38. return $return;
  39. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.