best way to get screen resolution php?
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
TonyMontana posted this at 17:52 — 23rd April 2004.
They have: 218 posts
Joined: Apr 2001
<?
$dim = "<script> sw=screen.width; sh=screen.height;</script>";
echo $dim;
?>
vixxiv posted this at 19:29 — 7th May 2004.
They have: 7 posts
Joined: May 2004
I'm not to familiar with php, but what does this particular script accomplish?
kb posted this at 02:21 — 8th May 2004.
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 = \"<script>sw=screen.width;</script>\";
$height = \"<script>sh=screen.height;</script>\";
?>
And then to add functionality, something like
<?php
If ($width == 1240) {
/* Do something */}
else { /*Do something else */};
?>
Greg K posted this at 04:37 — 8th May 2004.
He has: 2,145 posts
Joined: Nov 2003
<?php
$dim = \"<script> sw=screen.width; sh=screen.height;</script>\";
?>
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:
<script> sw=screen.width; sh=screen.height;</script>
'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 (\"<script>sw=screen.width;</script>\" == 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
kb posted this at 05:06 — 8th May 2004.
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 posted this at 17:59 — 8th May 2004.
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
-Greg
PS. I modified the code and placed the following at http://ctc.uakron.edu/greg/test02.php
<?php
$dim = \"<script> sw=screen.width; sh=screen.height;</script>\";
echo \"Variable <b>dim</b> is set to <b>\" . htmlspecialchars($dim) . \"</b><br>\n\";
$width = \"<script>sw=screen.width;</script>\";
$height = \"<script>sh=screen.height;</script>\";
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\";
?>
Suzanne posted this at 18:37 — 8th May 2004.
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.
TonyMontana posted this at 20:20 — 8th May 2004.
They have: 218 posts
Joined: Apr 2001
Yea, my approach in applying it was to stick within the javascript realm:
<?
$url = "newURL.php";
$dim = "<script> var sW=screen.width; sh=screen.height;";
$dim .= " location='$url'+'?sw='+sW+'&sh='+sh;";
$dim .= "</script>";
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 posted this at 21:24 — 8th May 2004.
She has: 5,507 posts
Joined: Feb 2000
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.
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.
kb posted this at 20:30 — 8th May 2004.
He has: 1,380 posts
Joined: Feb 2002
I no longer run the current page, so I could be mistaken...
My apologies...
TonyMontana posted this at 20:36 — 8th May 2004.
They have: 218 posts
Joined: Apr 2001
Eskater's script is not returning screen dimensions:
$width = "<script>sw=screen.width;</script>";
$height = "<script>sh=screen.height;</script>";
echo $width . " width"; // undefined
TM
TonyMontana posted this at 21:53 — 8th May 2004.
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 posted this at 22:19 — 8th May 2004.
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 posted this at 01:02 — 12th May 2004.
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
<script type=\"text/javascript\">
document.write '<img src=\"dimensions.gif?w='+screen.width+'&h='+screen.height+'&session='+session_id()+'\" alt=\"\" />';
</script>
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.