...

View Full Version : Detect screen resolution with Javascript and PHP



htcilt
03-26-2010, 04:22 PM
I'm trying to find a script that will detect the client's screen size so that I can do something like:


if ($screen_width > 1024){
function1();
else {
function2();
}

I've tried these examples:

http://www.wallpaperama.com/forums/php-tutorial-guide-how-to-show-screen-resolution-find-display-script-t517.html

http://www.phpbuddy.com/article.php?id=8

But my php page just keeps loading and nothing is displayed. It looks like its stuck in a loop? My guess is its not writing the cookie.

masterofollies
03-26-2010, 04:26 PM
This should work fine.


<HTML>
<TITLE>screen resolution</TITLE>
<HEAD>
<?
if(isset($HTTP_COOKIE_VARS["users_resolution"]))
$screen_res = $HTTP_COOKIE_VARS["users_resolution"];
else //means cookie is not found set it using Javascript
{
?>
<script language="javascript">
<!--
writeCookie();

function writeCookie()
{
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = 'get_resolution.php';
}
//-->
</script>
<?
}
?>
</HEAD>
<BODY>
<?php
echo "Your screen resolution is set at ". $screen_res;
?>
</BODY>
</HTML>

DaiLaughing
03-26-2010, 08:47 PM
Or you could just pass the resolution from a Javascript page using $_GET which is less attractive but easier:

If there is no info in $_GET do this:


window.location.href = 'thispage.php?w=' + screen.width + '&h=' + screen.height;

Else do this:


$height=$_GET['h'];
$width=$_GET['w'];

Then do what you want with that information.

Fou-Lu
03-26-2010, 09:42 PM
Yep, in either case you'll need JS to detect this; PHP has no idea what a screen is. Lol.
With Masterophollies suggestion, I'd actually follow that with JUST JS, no PHP at all. Also, PHP should use the $_COOKIE superglobal; HTTP_*_VARS are officially gone as of PHP6, and are currently disableable.
But, I do agree with cookies being used. Just use straight JS, since we know for 100% certainty that it will exist.

htcilt
03-26-2010, 10:37 PM
This should work fine.


<HTML>
<TITLE>screen resolution</TITLE>
<HEAD>
<?
if(isset($HTTP_COOKIE_VARS["users_resolution"]))
$screen_res = $HTTP_COOKIE_VARS["users_resolution"];
else //means cookie is not found set it using Javascript
{
?>
<script language="javascript">
<!--
writeCookie();

function writeCookie()
{
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = 'get_resolution.php';
}
//-->
</script>
<?
}
?>
</HEAD>
<BODY>
<?php
echo "Your screen resolution is set at ". $screen_res;
?>
</BODY>
</HTML>

I'd like to go with the JS/PHP solution as I can then build in my PHP functions.
However I cant get this code to work.
I've tried it on my office server and now at home using XAMPP. Both just go into some sort of loop. The status says 'waiting for 127.0.0.1', then 'stopped', and the loop continues.

I've looked at my cookies and one has been set with content of "1280x1024", so that part is working.

I commented out location = 'get_resolution.php'; to stop the forwarding loop back onto itself. As I know the cookie is set, it should still display the screen resolution, however it doesn't, just 'Your screen resolution is set at '.

So it looks like a problem reading back the cookie?

Fou-Lu
03-26-2010, 10:47 PM
Did you refresh the page? Cookies of course do not show up until the next load.
Also, did you change the $HTTP_COOKIE_VARS to $_COOKIE?

htcilt
03-26-2010, 10:52 PM
Found the problem.

It was a combination of not using the $_COOKIE Superglobal and not using <? instead of <?php.

I'm guessing the example was written in PHP4?

Here's the amended code:


<HTML>
<TITLE>screen resolution</TITLE>
<HEAD>
</HEAD>
<BODY>

<?php
if(isset($_COOKIE["users_resolution"]))
$screen_res = $_COOKIE["users_resolution"];
else //means cookie is not found set it using Javascript
{
?>
<script language="javascript">
<!--
writeCookie();

function writeCookie()
{
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = 'get_resolution.php';
}
//-->
</script>
<?php
}
?>


<?php
echo "Your screen resolution is set at ". $screen_res;
?>
</BODY>
</HTML>

htcilt
03-26-2010, 10:57 PM
I'm not too sure when to set the cookie to expire.
The problem is people can change their monitor and/or screen resolution at any time.

Does anyone have a recommendation on this?

Fou-Lu
03-26-2010, 11:08 PM
The code could have been written for pretty much any version; the difference is that you're server doesn't have short_tags enabled. Which is ok, I always recommend not using them for just these reasons.

As for detecting changes, only javascript can do that. Instead of using PHP to check that, use pure javascript and alter you're function to check if its cookies are not existant OR the sizes have changed compared to the cookie, update them.
And as mentioned, they will still only take effect after the next page load.

htcilt
03-26-2010, 11:18 PM
That all sounds good to me... but I haven't a clue how to do this. Can you help? :o

Fou-Lu
03-26-2010, 11:21 PM
Nope sorry. JS cookies are one big string, so you'll need to ask a JS writer for that; my JS skills are subpar.

htcilt
03-26-2010, 11:26 PM
Thanks anyway.
I'll hop over to the Javscript forum now I know its Javascript I'm needing.

cfructose
03-28-2010, 12:04 AM
@Fou-Lu


we know for 100% certainty that it will exist.

That what will exist?
I must have missed something, but that statement confused me.
:p

PS If "Fou-Lu" is Chinese (is it?), what are the tones on it? I was trying to guess the meaning, but there are so many possibilities!

DaiLaughing
03-28-2010, 08:49 AM
Or you could use my approach which means copy and pasting one line of Javascript into SCRIPT tags and then using PHP to $_GET it. That's why I suggested it as you don't need any Javascript knowledge for that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum