Wednesday, 3 June 2015

PHP: Server Side Browser Detection

A Set of functions to tell what type of browser a surfer is using 
<?php
function inAgent($agent) {    
 $notAgent = strpos($_SERVER['HTTP_USER_AGENT'],$agent) === false;
 return !$notAgent;
}

function isIE() {
 if (inAgent('MSIE') && !isOpera()) {
  return true;
 }
 return false;
}

function isSafari() {
 $br = new Browser;
 if ($br->Platform == "MacIntosh"||inAgent('Safari')) {
  return true;
 }
 return false;
}

function isFF() {
 if (inAgent('Firefox')) {
  return true;
 }
 return false;
}

function isNS4() {
 if ((!inAgent('MSIE')) && (inAgent('Mozilla/4'))) {
  return true;
 }
 return false;
}

function isIE7() {
 if (inAgent('MSIE 7') && !inAgent('Opera')) {
  return true;
 }
 return false;
}

function isOpera() {
 if (inAgent('Opera')) {
  return true;
 }
 return false;
}
?>

0 comments:

Post a Comment