Getting the screen resolution using PHP

Getting the screen resolution using PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Illusion of Direct Server-Side Resolution Access

There is a persistent misconception in web development that a server-side script, such as one written in PHP, can directly query a user's operating system or browser to retrieve their current screen resolution. If you attempt to access variables like $_SERVER['SCREEN_WIDTH'] or similar proprietary keys, you will find them empty. This is by design. For security and privacy reasons, the web server environment is deliberately stripped of direct access to client hardware specifications. PHP runs on the server; it cannot "see" the monitor attached to the user's machine without explicit cooperation from the browser itself via HTTP headers.

To actually obtain this data, you must shift the responsibility of measurement to the client-side JavaScript. The resolution information lives in the screen object within the browser context, specifically in properties like screen.width, screen.height, and screen.colorDepth. Since PHP cannot execute JavaScript directly on the client, it relies on the browser to calculate these values and send them back as a string via an HTTP request header.

The standard mechanism for this is the User-Agent header. While primarily used for browser identification, it often contains resolution data depending on how the browser constructs the string. However, relying solely on the raw User-Agent string is brittle. Different browsers format this information differently, and many privacy-focused configurations or extensions strip out specific hardware identifiers. A more robust approach involves creating a small client-side script that explicitly calculates the resolution and sends it to your server using an AJAX request before navigating away from the page.

Here is how you might structure the client-side logic in JavaScript. This snippet calculates the viewport dimensions (which often approximates screen resolution for full-screen sites) or true screen size depending on window sizing:

const resolutionData = {
    width: screen.width,
    height: screen.height,
    colorDepth: screen.colorDepth
};

// Send data to your server via AJAX or fetch
fetch('/api/collect-resolution', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(resolutionData)
});

On the server side, you can then parse this incoming JSON payload. In a vanilla PHP application, you would decode the JSON string using json_decode() and store the values in your database or log file. However, if you are working within the Laravel ecosystem, handling this data is streamlined through middleware or controllers that accept JSON payloads via request input.

// Inside a Laravel Controller method
public function collectResolution(Request $request) {
    $data = json_decode($request->input('body'), true); // Or parse based on your specific payload structure
    
    if ($data) {
        $userAgent = $request->header('User-Agent');
        // Logic to store or process resolution data
        \DB::table('user_sessions')->insert([
            'screen_width' => $data['width'],
            'screen_height' => $data['height'],
            'user_agent' => $userAgent,
            'created_at' => now(),
        ]);
    }
}

It is crucial to understand the trade-offs here. First, this data collection happens asynchronously. If a user refreshes the page or closes it immediately after loading, there may be a race condition where the resolution data never reaches your server before the session expires. Second, relying on screen.width gives you the full physical screen size, not necessarily the viewport size available to the web app if browser toolbars or address bars are present.

Furthermore, modern privacy standards and browser settings often restrict access to certain hardware properties. Some browsers may block cross-origin requests that attempt to send detailed device fingerprints without user consent. Additionally, sending this data every time a user visits your site can increase payload size and processing load if not handled efficiently.

If you require a more comprehensive solution that handles diverse client capabilities, session management, and secure data storage automatically, exploring managed solutions like those offered by LaravelCompany can provide additional infrastructure support for tracking metrics without reinventing the entire collection pipeline. Ultimately, however, the fundamental limitation remains: the server must ask the client what its resolution is, because the server never has direct visibility into the client's hardware.

Our network