how to get user's screen resolution with PHP

how to get user's screen resolution with PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Why PHP Cannot Directly Access Screen Resolution

Trying to retrieve a user's screen resolution directly from a PHP script is a common misconception that often leads to dead ends in debugging sessions. The fundamental reason this operation fails is architectural: PHP runs on the server side, while screen resolution is a client-side property of the operating system and display hardware. There is no native getScreenResolution() function in PHP because the language engine has no visibility into the user's local environment once the request reaches your web server.

Attempting to access this data requires shifting the responsibility to the client. You must ask the browser to perform a calculation or query and then send the result back to the server via HTTP headers or JSON payloads. This approach introduces immediate trade-offs regarding accuracy, timing, and user consent.

The Client-Side Query Requirement

To obtain the width and height of a user's viewport, you must execute JavaScript within the browser context. The most direct method involves injecting a script that reads the window.innerWidth and window.innerHeight properties.

Since PHP cannot run these commands, you must embed the JavaScript logic directly into your HTML response or load it dynamically. A common pattern is to create an inline script that calculates the dimensions and outputs them as JSON, which can then be parsed by a server-side language like PHP using cURL or similar tools if running on the client side itself, or simply rendered back into the DOM for frontend use.

If you are building a web application where backend logic needs this data (for example, to render an image optimized for the device), you must handle the extraction within the browser first. Here is how the logic typically flows:

<script>
    const dimensions = {
        width: window.innerWidth,
        height: window.innerHeight
    };
    
    // Send data back to a PHP endpoint via AJAX or output as JSONP/JSON
    console.log('Screen Resolution:', dimensions);
</script>

Handling Header and Meta Data Limitations

Sometimes developers look for server-side headers that might contain this information, such as specific User-Agent strings. While the User-Agent header often identifies the device model (e.g., "iPhone14,2" or "Samsung Galaxy S23"), it rarely specifies the exact screen resolution in pixels.

Relying on User-Agent parsing to infer resolution is fragile and technically inaccurate. Different devices with the same physical screen size may report different resolutions depending on their operating system scaling settings (e.g., Retina displays often report a logical resolution lower than the physical pixel count). Therefore, you should not attempt to parse the $_SERVER['HTTP_USER_AGENT'] string in PHP to derive pixel dimensions.

The Meta Viewport Complication

Another frequent point of confusion involves the <meta name="viewport"> tag. While essential for responsive web design, this tag does not inherently report the screen resolution to the server. It only instructs the browser on how to scale the page relative to the device width.

In many scenarios, window.innerWidth includes the scrollbar width and any UI chrome (browsers often exclude these from the calculation when using standard viewport APIs, but logic can vary by browser). If you need precise pixel-perfect data for server-side image generation or layout calculations, you must account for the fact that the browser's reported dimensions are what matters, not the physical hardware specs.

Integration with Laravel and Modern Frameworks

When working with modern PHP frameworks like Laravel, the approach remains consistent: the backend cannot sniff the screen, but it can receive the data if the frontend sends it. You might create a dedicated API endpoint that accepts JSON containing these dimensions.

// Example of handling incoming resolution data in a Laravel controller
public function store(Request $request) {
    // Validate that the client sent the dimensions
    $width = $request->input('screen_width');
    $height = $request->input('screen_height');
    
    if ($width && $height) {
        // Proceed with logic using the received dimensions
        return response()->json(['success' => true]);
    }
    
    return response()->json(['error' => 'Screen resolution not provided'], 400);
}

If you are looking for a streamlined way to handle such cross-client/server interactions without reinventing the wheel, resources like https://laravelcompany.com often provide insights into best practices for managing client-server data flow in modern stacks. However, the core principle remains: the server must be told what the screen is, it cannot guess.

Performance and Privacy Considerations

Fetching screen resolution adds a minimal overhead to the initial page load if implemented via inline scripts, but it does add a dependency on JavaScript execution. If your goal is purely server-side optimization (like serving different images), consider that you can also use CSS media queries in combination with srcset attributes to serve appropriate assets without ever needing to know the exact resolution in PHP.

From a privacy standpoint, transmitting screen dimensions is generally considered low-risk PII, but it is still data about the user's device configuration. Ensure your privacy policy reflects that you may collect device characteristics if you are logging this information on the backend.

Our network