How can I get the screen resolution with PHP?

BR
BR used Ask the Experts™
on
Dear Experts,

I need to write below the below code in PHP, how can I get the screen resolution in PHP ?

   <script type="text/javascript">
  
 if (screen.width >= 950) {
  window.location = "https://www.mysite.com/";
  }
  
</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Principal Software Engineer
Commented:
Can't be done directly in PHP.  Javascript is needed; see the link below.

It is worth noting that the value returned is only what the browser claims and that is not necessarily what the actual screen size is.  More and more browsers are restricting the use of this information or spoofing it so that user/system tracking via browser profiling doesn't work.

As those authors note, what you want is probably the browser viewport size, not the screen size.  If the browser window is 400 pixels wide and the screen is 4000 pixels, then rendering for a 4000 pixel width won't make anyone pleased with the result.

https://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php
Terry WoodsIT Guru
Most Valuable Expert 2011
Commented:
It's worth explaining WHY it's not possible directly with PHP: it's because PHP is a server-side language. It generates content to send to the user's browser. The generated content can include HTML, CSS, Javascript and other content that browsers can understand, but browsers don't understand PHP code so there's no PHP running on the client side. Once the content has been sent to the client, it can tell the server what the resolution should be using, for example, Javascript.

Generally for a responsive website, CSS media queries (and other techniques) enable the server to send content that is essentially appropriate for all possible screen resolutions, and the browser uses that to determine which resources to load (eg a big image or a little one).
BRDigital Marketing

Author

Commented:
thank you both.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial