Click here to Skip to main content
Full site     10M members (34.6K online)    

Get Client Resolution Data with PHP

Introduction

In PHP, client resolution data is not accessible directly, but in JavaScript, screen width & height is accessible directly! In these two languages, we can set and get cookies, so, we can set resolution data in a cookie with JavaScript and receive it with PHP!

Using the Code

Ok, now we should get client resolution data with JavaScript. For getting this data in all browsers, we should define a cross-browser code to get screen width and height.

if(typeof(window.innerWidth) !== 'number') {
    if(document.documentElement.clientWidth !== 0) {
        width  = document.documentElement.clientWidth;
        height = document.documentElement.clinetHeight; 
    }
    else {
        width  = document.body.clientWidth;
        height = document.body.clinetHeight;
    }
}
else {
    width  = window.innerWidth;
    height = window.innerHeight;
}  

Width & height variables are client resolution data.

We should set two cookies, width and height:

document.cookie = 'width' + '=' + width;
document.cookie = 'height' + '=' + height; 

Now, this data is stored in cookies and we can access these cookies with PHP.

For example:

echo $_COOKIE['width'];

Notice!

For first load, client resolution data is not accessible with PHP, because JavaScript should set this data in cookies.

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
-- There are no messages in this forum --

Last Updated 12 May 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013