0

I'm interested in using a php if statement to include or exclude code based on the user's browser size. For example, a slider will only load if the user's browser size is greater than 768 x 900px.

I know how to hide an object using the css viewport, but I imagine the page speed would increase if I could simply not load the code at all, rather than hiding it.

A few questions so I can better understand

1) Is PHP capable of detecting a user's browser size?
2) Is an if statement a feasible way of achieving this?
3) Am I correct in assuming that not loading the code altogether is more efficient than just hiding the output via css?
4) I haven't started learning javascript yet, would you recommend that as the best way to achieve this?

Any help is really appreciated!

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
mn8809
  • 454
  • 1
  • 5
  • 13
  • 1
    1) No, you'd need to use client side javascript to read the screen size and send that information to PHP, and watch out for user's changing their screen size subsequently – Mark Baker Oct 23 '14 at 18:13
  • 2) Not really, hiding/resizing/etc is better handled on the client browser using javascript anyway – Mark Baker Oct 23 '14 at 18:14
  • 3) Not really, you're adding a lot of overhead to both browser and server code to see if it's worth displaying or not, compared with negligeable overhead on the client using css/js to hide it – Mark Baker Oct 23 '14 at 18:16
  • @MarkBaker 2) Who on Earth uses JavaScript for screen size detection? Use a media query! – DividedByZero Oct 23 '14 at 18:16
  • 4) Yes I would recommend learning js – Mark Baker Oct 23 '14 at 18:17
  • @RandomUser - does a media query from PHP actually tell you the browser window size? I am clearly woefully out of date – Mark Baker Oct 23 '14 at 18:17
  • @MarkBaker I'm talking about client side CSS. – DividedByZero Oct 23 '14 at 18:18
  • @RandomUser - doesn't negate my point that the client needs to send that information to PHP if the "hiding" is to be done in PHP, and if that information has to be sent to PHP then it's a hopelessly inefficient way of working – Mark Baker Oct 23 '14 at 18:20
  • @MarkBaker Why don't you post your insights as an answer? – Scimonster Oct 23 '14 at 18:20

2 Answers2

4
  1. PHP is not capable of detecting the user's browser size. The PHP is executed before there even is a user, as it's on the server.

  2. ...

  3. If you have tons of code, then yes, it might be more efficient, but a single slider should not make such a big difference. I would recommend just loading it, and hiding via CSS.

  4. As i mentioned in the above bullet point, CSS is going to be more efficient than JavaScript. Just use an @media query:

    @media (min-width: 768px) and (min-height: 900px) {}
    
Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

If you want to detect the screen size of your client, PHP can't help you, javascript and CSS can.

If you are concern about speed, javascript and CSS can't help you (even if it is minified), PHP can.

My suggestion is, when the client visits your website for the first time, detect their screen size using CSS and after that, save it in a database and set a cookie/session that corresponds with it.

The problem with this workaround is when the client change their screen size before the cookie expires or the session ends.

jsp
  • 1
  • Even something as simple as turning my browsing device from landscape to portrait (or vice versa) will cause problems – Mark Baker Oct 23 '14 at 21:03