best way to get screen resolution php?

They have: 5,633 posts

Joined: Jan 1970

Hi,

i would like to know the best/simplest way to retrieve screen resolution.
i need it to format the sizes of newsbox.
the language is php, and i think i will need javascript.

any input welcome.
thanks
tias

They have: 218 posts

Joined: Apr 2001

<?
$dim = "&lt;script&gt; sw=screen.width; sh=screen.height;&lt;/script&gt;";
echo $dim;
?>
'

They have: 7 posts

Joined: May 2004

I'm not to familiar with php, but what does this particular script accomplish?

He has: 1,380 posts

Joined: Feb 2002

step 1: it sets the variable 'dim' to a javascript, which detects the screen width and screen hight

step 2: displays the contents of 'dim', being for example:

640
480
'

If you want to use it for something that matters, you should do

<?php
$width
= \"&lt;script&gt;sw=screen.width;&lt;/script&gt;\";
$height = \"&lt;script&gt;sh=screen.height;&lt;/script&gt;\";
?>

And then to add functionality, something like

<?php
If ($width == 1240) {
 
/* Do something */}
else {
/*Do something else */};
?>

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

<?php
$dim
= \"&lt;script&gt; sw=screen.width; sh=screen.height;&lt;/script&gt;\";
?>

This line assigns the actual text that you see in beweteen the quotes to the variable $dim. This sets it to be Javascript code, not EXECUTED javascript. Therefore:

<?php
echo $dim;
?>

results in the following:
&lt;script&gt; sw=screen.width; sh=screen.height;&lt;/script&gt;'
being sent to the browser, NOT the values of the screen size

In the last sample given:

<?php
If ($width == 1240) {
 
/* Do something */}
else {
/*Do something else */};
?>

the if statement will evaluate to be:

<?php
if (\"&lt;script&gt;sw=screen.width;&lt;/script&gt;\" == 1240)
?>

which will always evaluate to be false. (the variable $width only contains the text, not the values as indicaded by the previous post)

In order to use the height and width, you need to do all checking with javascript.

-Greg

He has: 1,380 posts

Joined: Feb 2002

I beg to differ....

I set it up on a website I used before very similar to my post, and all was copasetic.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

What version of PHP and web server were you using? Before posting my responce, to confirn what I thought, I placed the code by TonyMontana into a file on a server (SuSE Linux, Apache 2, PHP 4.3.3) and called it. (you can try it: http://ctc.uakron.edu/greg/test01.php ).

When the page was called, I did a view source, and the following is what I mentioned above. (Also received same responce on another server where I have hosting using FreeBSD and apache 1.x PHP 4.x).

For this to work, the browser would have to send BACK to the server the results of the javascript so it could be executed in the PHP.

I'll admit, I may be missing something, and if I am, I'd like to learn it Wink

-Greg

PS. I modified the code and placed the following at http://ctc.uakron.edu/greg/test02.php

<?php
$dim
= \"&lt;script&gt; sw=screen.width; sh=screen.height;&lt;/script&gt;\";
echo \"Variable <b>dim</b> is set to <b>\" . htmlspecialchars(
$dim) . \"</b><br>\n\";

$width = \"&lt;script&gt;sw=screen.width;&lt;/script&gt;\";
$height = \"&lt;script&gt;sh=screen.height;&lt;/script&gt;\";

echo \"Variable <b>width</b> is set to <b>\" . htmlspecialchars(
$width) . \"</b><br>\n\";
echo \"Variable <b>height</b> is set to <b>\" . htmlspecialchars(
$height) . \"</b><br>\n\";
?>
and this is the responce I got back:

Quote: Variable dim is set to <script> sw=screen.width; sh=screen.height;</script>
Variable width is set to <script>sw=screen.width;</script>
Variable height is set to <script>sh=screen.height;</script>

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

The PHP will be "compiled" on the server, THEN sent to the browser where the JavaScript will be executed. It will then be available for JavaScript to control the page, but not PHP.

Whenever possible, don't rely on screen sizes for site design. If you really need to, put it in the power of the user (who may not be using full-screen, for instance to view your site and would have worse viewing problems!) and set a cookie for the preferences.

They have: 218 posts

Joined: Apr 2001

Yea, my approach in applying it was to stick within the javascript realm:

<?
$url = "newURL.php";
$dim = "&lt;script&gt; var sW=screen.width; sh=screen.height;";
$dim .= " location='$url'+'?sw='+sW+'&sh='+sh;";
$dim .= "&lt;/script&gt;";
echo $dim;
?>
'

>>don't rely on screen sizes for site design

These are useful markers for centering content in dynamic design which I use to determine the borders around content.

TM

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

TonyMontana wrote: >don't rely on screen sizes for site design

These are useful markers for centering content in dynamic design which I use to determine the borders around content.

Right, so that falls into the "really need to" category, and hopefully with JavaScript turned off you have it so the design will fail gracefully. Wink

But again, screen width is NOT necessarily what people have set as their browser width and a lot of people get alarmed and annoyed when their browser is resized.

I run a couple of windows with many tabs -- if you resize my browser, I have to PUT IT BACK to read my other tabs (often forums) at my preference. Trust me when I say this just makes me not think kindly of the person who deemed it necessarily to over-ride my preferences on a matter of ego.

If you really, really believe that the window has to be different from what I have, give me the option through a link or form button to do the resizing if I happen to agree with you.

He has: 1,380 posts

Joined: Feb 2002

I no longer run the current page, so I could be mistaken...

My apologies...

They have: 218 posts

Joined: Apr 2001

Eskater's script is not returning screen dimensions:

$width = "&lt;script&gt;sw=screen.width;&lt;/script&gt;";
$height = "&lt;script&gt;sh=screen.height;&lt;/script&gt;";

echo $width . " width"; // undefined
'

TM

They have: 218 posts

Joined: Apr 2001

>>>a lot of people get alarmed and annoyed when their browser is resized.

New windows can/should be resized to whatever the developer/client wants, imo. For instance, if a new video window opens, and the content is 240X160, the browser needs to be resized.

TM

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

If it's a new window, it should be opened to whatever size you need, sure. But it shouldn't check to see that it has and then force a resize if it's been opened in a tab instead, do you see what I mean?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Browsers do not send screen or window dimensions in the HTTP Request. You must use a client side scripting language to send the information to the server.

There are a few tricks.

First, if you can get the information (dimensions) you need on a page before the it is required, you could store the information in a session. They will then be available for later use, when you dynamically build your sized html.

To get the information in this mannor, I recommend use of an img tag, such as:

<?php
echo<<<myHTML
&lt;script type=\"text/javascript\"&gt;
document.write '<img src=\"dimensions.gif?w='+screen.width+'&amp;h='+screen.height+'&amp;session='+session_id()+'\" alt=\"\" />';
&lt;/script&gt;
myHTML;
?>

Second, if you need the information *now*, you can use the img technique above combined with a delay. Print the page as normal with the img tag as close to the top as you can. Stop printing when you require the dimensions/information. Enter a loop that pauses (with sleep[250]) and checks to see if the information has obtained (in a database entry linked to the users session), or a given time (2 seconds) has elapsed. Then resume printing you html source to the client (with or without your dimensions/information).

This works because most browsers will download images even as the html source is still being downloaded. Pausing the output of your html source, your browser should be able to request your special img tag. When it does, you can enter the information into a database entry tied to a users session.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.