65.9K
CodeProject is changing. Read more.
Home

PHP browser detector

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jul 3, 2011

CPOL
viewsIcon

18730

Code to detect browser name in PHP.

The following code provides a simple PHP browser detection method which only echos the name of the browser used by the user.

<?php
$browser="";
     if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("MSIE"))){$browser="ie";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("Presto"))){$browser="opera";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("CHROME"))){$browser="chrome";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("SAFARI"))){$browser="safari";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("FIREFOX"))){$browser="firefox";}
else {$browser="other";}
echo $browser;
?>

Note: I have posted this tip in order to help beginners like myself as I have faced this same problem. Please feel free to correct me if I am wrong.