A way to determine browser width in PHP without javascript?

aslum picture aslum · Feb 14, 2010 · Viewed 42.1k times · Source

First off is there one? Or would I have to use javascript? I'd like to be able to make changes to which CSS is used, so frex I could load smaller fonts for a mobile device, or whatever.

Answer

Sampson picture Sampson · Feb 14, 2010

Unfortunately there is no way to detect the users resolution with PHP only. If you use Javascript, you could set this value in a cookie, and all subsequent requests could check the value of that cookie. This seems to be a pretty popular method for those working with this issue.

You could also run a small javascript from the page that checks to see if a resolution-cookie is set. If it's not, it sends an asynchronous request to the server containing the screen resolution. The server determines which CSS file to use by this value, and sends its path back to the javascript. A cookie is then set to indicate resolution has been determined, and the css file is subsequently loaded (via javascript) into the page. All future requests would cease assuming they're contingent upon the resolution cookie.