How to Detect Browser in PHP & JavaScript

Knowing the user’s browser can be incredibly useful when building modern web applications-whether you want to apply specific CSS fixes, optimize functionality, or show compatibility messages. Being able to detect browsers in PHP and JavaScript helps you give your visitors a seamless experience.

Moreover, when, for example, an end user browses a web application using an old browser such as Internet Explorer, a friendly notice should appear and advise them to switch to a more secure browser type. Practical means will ensure consistency of your website on different platforms and devices. In this article, let’s see how to detect browser type and version in JavaScript and PHP.

Detecting Browser using JavaScript

JavaScript runs in the user’s browser and, therefore, is the ideal choice to detect browser information instantly. Through the navigator.userAgent property, a developer can detect which browser name and version and operating system are in use.

Let’s take a simple example to detect browser and user agent using JavaScript:

let userAgent = navigator.userAgent;
let browserName;
let browserVersion;

if (userAgent.includes("Chrome")) {
  browserName = "Google Chrome";
} else if (userAgent.includes("Firefox")) {
  browserName = "Mozilla Firefox";
} else if (userAgent.includes("Safari") && !userAgent.includes("Chrome")) {
  browserName = "Apple Safari";
} else if (userAgent.includes("Edge")) {
  browserName = "Microsoft Edge";
} else {
  browserName = "Unknown Browser";
}

console.log("Browser Name:", browserName);
console.log("User Agent:", userAgent);

Output:

Browser Name: Google Chrome  
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

As you can see, this script will get user’s browser name and user agent using JavaScript Navigator library.

Get Browser Using PHP

While JavaScript detects browsers on the client side, you can also fetch it using PHP on the server side. It is useful in cases when you have to log browser data or customize backend responses based on the user’s browser. For knowing user behavior browser is idle.

<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($userAgent, 'Chrome') !== false) {
    $browser = 'Google Chrome';
} elseif (strpos($userAgent, 'Firefox') !== false) {
    $browser = 'Mozilla Firefox';
} elseif (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Chrome') === false) {
    $browser = 'Apple Safari';
} elseif (strpos($userAgent, 'Edge') !== false) {
    $browser = 'Microsoft Edge';
} else {
    $browser = 'Unknown Browser';
}

echo 'Browser Name: ' . $browser;
?>

Output:

Browser Name: Google Chrome

This PHP script extracts the HTTP_USER_AGENT from the request header and checks for common browser names. A real-world example could be a CMS system tracking the type of browsers visiting the dashboard.

Conclusion

Detect Browser in PHP and JavaScript allows you to offer a customized user experience, collect insights into usage, and ensure better performance for all platforms. With JavaScript offering instant detection and PHP offering a method of implementing back end logic.