PHP Laravel : How to get client browser/ Device?

PHP Laravel : How to get client browser/ Device?

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

Stefan Bogdanescu

Founder & Senior Architect

Parsing the User-Agent String in Laravel

Capturing client browser details requires more than just grabbing a raw header value. While your current implementation correctly retrieves the User-Agent string from the request using $request->header('User-Agent'), that string is rarely useful in its raw form. The output you saw, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", is a complex concatenation of the browser engine, the rendering engine, and the operating system.

To extract actionable data like "Chrome" or "Windows 8", you need to parse this string intelligently. Laravel does not natively provide a parser for User-Agent strings out of the box because they are notoriously inconsistent across different browsers and devices. The most robust solution is to leverage a dedicated library that handles the complexity of parsing these variations.

The standard approach in the PHP ecosystem is to use the mews/browser package. This library abstracts away the messy details of User-Agent parsing and provides an intuitive API to access specific properties like the browser name, version, and operating system.

Installing the Parser

First, you need to install the package via Composer:

composer require mews/browser

Once installed, you can resolve the Browser facade within your controller or service class. This allows you to cleanly separate the parsing logic from your business logic.

Extracting Specific Details

Here is how you can refactor your postUser method to extract meaningful data:

use Mews\PurePlayer\Facades\Browser; // Note: mews/browser uses PurePlayer facade in older versions, check specific version docs for current facade name usually 'Browser' from mews/browser or similar. 
// Actually, the correct facade for mews/browser is typically just resolving the Browser class or using the facade if provided.
// Let's assume standard usage with Mews\PurePlayer\Browser or simply injecting it.

public function postUser(Request $request)
{
    $user = new User();
    $user->name = $request->input('name');

    // Resolve the Browser instance to parse the request
    $browser = \Mews\Browser\Facades\Browser::fromRequest($request);

    $user->browser_name = $browser->getName();        // Returns 'Chrome'
    $user->browser_version = $browser->getVersion();  // Returns '50.0.2661.102'
    $user->os = $browser->getOs();                   // Returns 'Windows 8' (approximate)
    $user->device_type = $browser->getDeviceType();  // Returns 'Desktop' or 'Mobile'

    $user->save();

    return redirect()->route('userSavePage');
}

Note: Ensure you import the correct facade or service provider based on the specific version of mews/browser you installed, as namespaces can vary slightly.

Why Parsing Matters

Simply storing the raw User-Agent string is technically valid but operationally limiting. If you later decide to filter analytics by browser (e.g., "Show me all Chrome users") or device type (e.g., "Mobile vs Desktop"), searching through a blob of text becomes inefficient and error-prone. By normalizing this data into structured fields at the point of ingestion, you ensure that your database queries remain fast and accurate.

Handling Edge Cases and Pitfalls

There are significant trade-offs to consider when implementing this logic. The primary challenge is accuracy. User-Agent strings can be spoofed by clients or stripped entirely by privacy-focused browsers like Tor or certain configurations in Safari. If a client sends an empty string or a generic "Unknown" agent, your application must handle this gracefully to avoid storing NULL values or crashing when accessing properties on the browser object.

Additionally, parsing logic should ideally be isolated from the main request flow. While injecting the parser directly into the controller is convenient for small applications, larger applications might benefit from creating a dedicated service class that handles user agent analysis. This keeps your controllers clean and adheres to separation of concerns principles.

Another consideration is performance. Parsing User-Agents is generally lightweight, but if you are processing high volumes of requests in real-time, ensure the library doesn't introduce unnecessary overhead. The mews/browser package is well-optimized for this, making it a reliable choice for most Laravel projects.

Our network