PHP: Detecting Google Chrome Browser

Posted on October 6, 2008, under PHP,  Bookmark it

This is a short snippet useful to detect if the visitor uses Google Chrome Browser.

<?php
function is_chrome()
{
return(eregi("chrome", $_SERVER['HTTP_USER_AGENT']));
}

if(is_chrome())
{
// code for Chrome Browser here

echo 'You are using Google Chrome Browser.';
}
?>

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

10 Replies to "PHP: Detecting Google Chrome Browser"

  1. I dont fully understand the need for this?

  2. This code is great if you wish to display one set of output for Chrome, and another for (say) Firefox or Internet Explorer.

    Thanks for providing this code… it really saved me today!

    1. @Eric Wood,

      Thanks Eric, now i understand.

  3. Thx! This was just what i needed, and very simple to.

    I use it because chrome of some reason wants a differen charset, than IE, and FF in some cases. (Too display special characters, when using AJAX, PHP, HTML, and CSS).

    Thx alot!

  4. May be handy but strstr is significantly faster and more efficient than using eregi.

    You should never use regular expression functions unless you’re using regular expressions.

    Also this eregi is depreciated as of PHP 5.3.0 (php.net).

  5. This one is useful in Joomla Backend, JCE is messed up in google chrome

  6. Thanks for the code – works perfectly!

  7. This code:

    return(eregi(“chrome”, $_SERVER['HTTP_USER_AGENT']));

    should be:

    return(preg_match(“/applewebkit/i”, $_SERVER['HTTP_USER_AGENT']));

    First, the eregi function calls in PHP are deprecated and shouldn’t be used.
    And second, the check should be for ‘applewebkit’, since it will match other browsers like ‘chromium’, ‘safari’, ‘midori’, etc. These browsers use the same rendering engine as chrome and will suffer from whatever problems one is working around by these conditional checks.

    Peace

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)