How to detect browser in PHP?

Sometimes you need to detect a user’s browser from PHP for the purposes of displaying browser specific content or adjusting CSS & HTML.

PHP’s $_SERVER global variable holds browser’s ID information in HTTP_USER_AGENT key: $_SERVER['HTTP_USER_AGENT'].

$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (preg_match('/MSIE/i', $user_agent)) {
    echo "Internet Explorer";
} else {
    echo "Non-IE Browser";

Better alternative would be also to use PHP function get_browser:

$browser = get_browser(null, true);

Libraries to check out when you need some advanced functionalities: