How to get user agent in PHP

How to get user agent in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting User-Agent Data in PHP: Moving Beyond navigator.appName

Your current JavaScript approach relies on navigator.appName, a property that dates back to the early days of web browsing. While it might have returned "Netscape" or "Mozilla" decades ago, modern browsers deprecated this specific identifier long ago. Today, navigator.appName typically returns just "Netscape" for almost every browser, including Chrome and Firefox, making it useless for distinguishing between Internet Explorer, Safari, or Edge.

To replicate the logic you desire in PHP, you must access the raw User-Agent header sent by the client. This header is a string containing a fingerprint of the browser version, operating system, and rendering engine. In PHP, this data is available via the $_SERVER['HTTP_USER_AGENT'] superglobal.

Accessing the Raw Header

The first step is to retrieve the raw string from the server environment. You can do this directly in your script:

$userAgent = $_SERVER['HTTP_USER_AGENT'];

if ($userAgent) {
    // Proceed with parsing
}

Once you have the string, you need to parse it to extract specific identifiers. The structure of a User-Agent string is generally consistent across browsers, usually following this pattern: Browser/Version (OS; Platform). For example, an older Internet Explorer 10 on Windows might look like this:

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)

To check if the user is using Internet Explorer, you need to look for specific keywords within that string. The most reliable method has historically been checking for MSIE (Microsoft Internet Explorer) or Trident. Note that modern versions of IE (Edge mode) often include Edg/ as well, so simple string matching requires care.

Implementing the Logic

Here is how you can implement the conditional logic you asked about, adapted for modern reality:

$userAgent = $_SERVER['HTTP_USER_AGENT'];

$isIE = strpos($userAgent, 'MSIE ') !== false || strpos($userAgent, 'Trident/') !== false;

if ($isIE) {
    // Handle Internet Explorer logic here
} else {
    // Handle other browsers
}

In this example, we use strpos() to search for the substring. If the function returns a boolean (false if not found), we can determine the browser family. However, relying solely on MSIE is risky because newer versions of Edge sometimes emulate IE while sending an "Edge" string in the header. A more robust check often involves looking for the specific engine token Trident, which was exclusive to IE.

Why Not Use Built-in Helpers?

You might be tempted to use functions like $_SERVER['HTTP_ACCEPT_LANGUAGE'] or rely on third-party libraries. While libraries like Browser-Detect exist, they can become bloated and slow down your application if overused. Parsing the string yourself is often more transparent and easier to debug when a specific browser version changes its headers.

The Real-World Limitation

It is important to acknowledge that parsing User-Agent strings is inherently fragile. Browser vendors frequently update these strings without warning, sometimes dropping legacy tokens like MSIE entirely even in compatibility mode. This is why modern web development often prefers Server-Side Rendering (SSR) frameworks or specific middleware packages designed to handle user-agent detection with less effort.

If you are using the Laravel framework, this task is significantly easier because it provides a dedicated helper: Request::agent(). Laravel parses the header for you and returns a standardized object containing properties like isMobile, platform, and browser. This removes the need to manually write regex or strpos chains, reducing the risk of errors when browser headers evolve.

For more complex detection needs, consider leveraging existing community resources or consulting documentation like https://laravelcompany.com for best practices on handling request data in modern PHP stacks.

Ultimately, while checking for "Internet Explorer" via $_SERVER['HTTP_USER_AGENT'] is technically possible using string manipulation, it is a temporary fix. The industry standard has shifted toward detecting the rendering engine directly or relying on framework helpers that abstract away these inconsistencies.

Our network