PHP – Detecting Firefox Browser

The code below shows you the way how to detect if a browser is Firefox or not using PHP.

To do this, we simply look at User-Agent data ($_SERVER ['HTTP_USER_AGENT'])  if it contains firefox string, the browser is Firefox. Otherwise, the browser is something else, it can be Chrome, Edge, Safari, or whatever.

The code looks simple as this:


function detectFirefox() {
    $userAgent = strtolower($_SERVER ['HTTP_USER_AGENT']);
    return (false !== strrpos($userAgent, 'firefox'));

Use the function:

// Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0
if (detectFirefox()) {
    echo 'The browser is Firefox';
} else {
    echo 'The browser is not Firefox';


The browser is Firefox