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:

firefox-detection.php
<?php

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';
}

Output:

The browser is Firefox

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x