How can I get the screen resolution with PHP?

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

LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dr. KlahnPrincipal Software EngineerCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Terry WoodsIT GuruCommented:
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).
1
BRMarketingAuthor Commented:
thank you both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.