Click here to Skip to main content
Click here to Skip to main content

Get Client Resolution Data with PHP

By , 11 May 2013
 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Reza Farhadian
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
I'm a persian software maker/developer & user interface designer.
I like creativity & open-source.
My dream language is javascript, i like to make & develop online applications!
 
My believe in web like real world, and i love it, I'm trying for design with art.
Coding for me is enjoyable & can't make me tired.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
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 this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 12 May 2013
Article Copyright 2013 by Reza Farhadian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid