Forum Moderators: coopster

Message Too Old, No Replies

how do I get screen resolution in PHP

         

michal317

12:56 am on May 20, 2006 (gmt 0)

10+ Year Member



I looked all over and the only function I found was:
GET[$_screen]
but it didn't work.
anyone has any idea how to use it?

jatar_k

1:08 am on May 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, what you have there wouldn't work and I am not sure what it is

looks like the screen res is passed in a $_GET var though I am not sure from where

you might be better off using javascript to get the screen res

ergophobe

1:19 am on May 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remember too that even if you do manage to grab the screen res with Javascript and put it in a URL and pass it to PHP as a GET param, that is only useful for the *next* page, not the *current* page.

The way you would need to do it is to have Javascript that appends the query string to the URL when the user clicks on it, but in any case the user would need to view one page before you could do anything with PHP.

TomAnthony

2:17 am on May 20, 2006 (gmt 0)

10+ Year Member



With Ajax, you could detect the resolution with Javascript, then send requests for the rest of the page, passing the resolution.

Having said that, you'd need to be careful with this, and not get too overzealous. Done right it could improve certain sites, done wrong, it could be a UI disaster.

Let us know how you get on!

FiRe

10:18 am on May 20, 2006 (gmt 0)

10+ Year Member



Put this on the page:

<script language="javascript">
window.location.href = "screen.php?width=" + screen.width + "&height=" + screen.height;
</script>

Then create a file called screen.php and use this:

<?php
$width = $_GET['width'];
$height = $_GET['height'];

echo "You are using a $width x $height screen resolution";
?>

rainborick

1:41 pm on May 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to check that the query string is empty/null:

<script language="javascript">
if (window.location.search == "") {
window.location.href = window.location + "?width=" + screen.width + "&height=" + screen.height;
}
</script>

to prevent an infinite loop.

henry0

11:11 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Rainborick
with the first option I see how the file screen.php is created
window.location.href = "screen.php?width=" + screen.width + "&height=" + screen.height;

but what is the file name/created with your method?

window.location.href = window.location + "?width=" + screen.width + "&height=" + screen.height;

Obviously I am not much of a JS person :)

Tastatura

3:36 pm on May 21, 2006 (gmt 0)

10+ Year Member



As an alternative to "GET" approach, you can write screen resolution to cookie with JS, and then read it "in" with PHP. Just something else to consider….