Knowledge

The Complete Guide to User-Agent: Browser Identity Secrets

A comprehensive guide to User-Agent strings, covering structure, historical evolution, practical applications, and how to parse and use User-Agent information.

When you visit a website, your browser quietly sends a mysterious string to the server, telling it “who I am.” This string is called the User-Agent (UA), one of the most important yet often overlooked technologies in the Web world.

This guide will take you deep into all aspects of User-Agent, from basic concepts to practical applications.

What is a User-Agent?

A User-Agent (abbreviated as UA) is a field in the HTTP request header that identifies the client software making the request. It contains information about the browser, operating system, device type, and more.

Typical User-Agent Example

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

This string looks complex, but it actually contains rich information:

  • Operating System: Windows 10 (NT 10.0)
  • Architecture: 64-bit (Win64; x64)
  • Browser Engine: WebKit/Blink (AppleWebKit/537.36)
  • Browser: Chrome 120
  • Compatibility Identifiers: Mozilla/5.0, Safari/537.36

The Historical Evolution of User-Agent

The complexity of User-Agent strings stems from the legacy of browser wars.

1. The Simple Early Days

Early browser User-Agents were very simple:

NCSA_Mosaic/2.0 (Windows 3.1)

2. The Rise of Netscape

When Netscape Navigator appeared, it added the “Mozilla” prefix to indicate Mosaic compatibility:

Mozilla/2.0 (compatible; MSIE 3.0; Windows 95)

3. Browser Spoofing

To achieve better website compatibility, browsers began to spoof each other:

  • IE pretended to be Mozilla
  • Chrome pretended to be Safari
  • Safari pretended to be KHTML
  • All modern browsers claim to be “Mozilla/5.0”

This led to the long and confusing User-Agent strings we see today.

User-Agent Structure Analysis

Let’s analyze a modern browser’s User-Agent in detail:

Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1

Components:

  1. Mozilla/5.0: Legacy compatibility identifier
  2. (iPhone; CPU iPhone OS 16_0 like Mac OS X): Device and OS information
  3. AppleWebKit/605.1.15: Rendering engine and version
  4. (KHTML, like Gecko): Engine compatibility declaration
  5. Version/16.0: Safari version number
  6. Mobile/15E148: Mobile version identifier
  7. Safari/604.1: Browser identifier

Practical Applications of User-Agent

1. Content Adaptation

Websites can provide different content based on User-Agent:

  • Mobile Adaptation: Detect mobile devices and return mobile-optimized pages
  • Browser Compatibility: Provide fallback solutions for older browsers
  • App Downloads: Recommend appropriate app downloads based on OS

2. Statistical Analysis

By analyzing User-Agent, you can understand:

  • Browser distribution of visitors
  • Operating system proportions
  • Mobile vs. desktop access ratios
  • Usage of specific browser versions

3. Security Protection

  • Bot Detection: Detect and filter malicious crawlers
  • Anomaly Detection: Identify suspicious access patterns
  • Access Control: Restrict access from specific clients

4. Feature Detection

While not recommended, sometimes it’s necessary to determine browser feature support based on User-Agent.

Common Browser User-Agents

Chrome (Windows)

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Firefox (Windows)

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0

Safari (macOS)

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15

Edge (Windows)

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

Mobile Chrome (Android)

Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36

Mobile Safari (iOS)

Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1

Getting and Parsing User-Agent in Code

JavaScript (Browser-side)

// Get current browser's User-Agent
const userAgent = navigator.userAgent;
console.log(userAgent);

// Simple device detection
const isMobile = /Mobile|Android|iPhone/i.test(userAgent);
const isChrome = /Chrome/i.test(userAgent);
const isFirefox = /Firefox/i.test(userAgent);

Node.js (Server-side)

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    const userAgent = req.headers['user-agent'];
    console.log('User-Agent:', userAgent);
    res.send('Hello!');
});

Python (Flask)

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    user_agent = request.headers.get('User-Agent')
    print(f'User-Agent: {user_agent}')
    return 'Hello!'

PHP

<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];
echo "User-Agent: " . $userAgent;
?>

User-Agent Parsing Libraries

Manually parsing User-Agent strings is complex; it’s recommended to use professional parsing libraries:

JavaScript

  • ua-parser-js: Powerful UA parsing library
  • bowser: Lightweight browser detection library
import UAParser from 'ua-parser-js';

const parser = new UAParser();
const result = parser.getResult();

console.log(result.browser.name);    // "Chrome"
console.log(result.browser.version); // "120.0.0.0"
console.log(result.os.name);         // "Windows"
console.log(result.device.type);     // "mobile" or undefined

Python

  • user-agents: Python UA parsing library
  • ua-parser: Cross-language UA parsing solution
from user_agents import parse

ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)...'
user_agent = parse(ua_string)

print(user_agent.browser.family)  # "Mobile Safari"
print(user_agent.os.family)       # "iOS"
print(user_agent.is_mobile)       # True

The Future of User-Agent: Client Hints

Due to increasingly bloated User-Agent strings and privacy concerns, browser vendors are promoting a new standard: User-Agent Client Hints (UA-CH).

Problems with the Traditional Approach

  • Strings are too long, wasting bandwidth
  • Contains too much information, potentially used for fingerprinting
  • Difficult to parse and maintain

Advantages of Client Hints

  • On-demand: Servers only request needed information
  • Privacy Protection: Reduces default exposed information
  • Structured Data: Uses separate HTTP headers, easy to parse

Client Hints Example

Sec-CH-UA: "Chromium";v="120", "Google Chrome";v="120"
Sec-CH-UA-Mobile: ?0
Sec-CH-UA-Platform: "Windows"

Servers can request more detailed information via the Accept-CH header:

Accept-CH: Sec-CH-UA-Full-Version, Sec-CH-UA-Platform-Version

Best Practices

1. Prioritize Feature Detection

Don’t rely on User-Agent to determine browser capabilities; use feature detection:

// ❌ Not recommended
if (userAgent.includes('Chrome')) {
    // Use some feature
}

// ✅ Recommended
if ('serviceWorker' in navigator) {
    // Use Service Worker
}

2. Use Professional Libraries on Server-side

Don’t write your own regex to parse User-Agent; use mature parsing libraries.

3. Consider Privacy Issues

Don’t excessively collect and store User-Agent information; comply with privacy regulations.

4. Prepare to Migrate to Client Hints

Follow the development of User-Agent Client Hints and gradually migrate.

Useful Tool

Want to quickly analyze User-Agent strings? We provide a convenient online tool: 👉 User-Agent Analyzer

This tool helps you:

  • Parse any User-Agent string
  • Identify browser, operating system, device type
  • View detailed version information
  • Test different User-Agents

Conclusion

Although User-Agent appears to be just a simple string, it carries the history of Web development and is an indispensable part of modern Web applications. Understanding how User-Agent works and best practices can help you build better Web applications.

With the promotion of User-Agent Client Hints, future User-Agents will become more concise and privacy-focused. As developers, we should embrace these changes and build more modern Web applications.