bahattab
28-01-2008, 04:40 PM
Getting Screen resolution using JavaScripts & PHP
http://www.phpbuddy.com/images/dot.gif
Overview: In this article we will discuss how to get the users screen resolution. Wouldn’t it be great if you get the users screen resolution so that you could send optimized content for that particluar resolution, we can get users resolution using JavaScript’s but the problem is how do we get PHP to recognize it.
What we can do is get the users resolution using JavaScript and then set a cookie which has the users resolution, we can then access that cookie in PHP!
How can I get the users screen resoultion using JavaScript?
It's really simple I will show you the code
screen.width; //gives you the screen width
screen.height; //gives you the screen height
I will explain you the stuff with an example We will be using get_resolution.php with the help of JavaScript we will get the screen resolution and then set the screen resolution's value in a cookie named users_resolution when ever we execute the page we check with PHP to see if the cookie users_resolution is set, if it is not set we set it using JavaScript.
get_resolution.phphttp://www.phpbuddy.com/icon/run.gifView Sample Output (http://www.phpbuddy.com/sample/get_resolution.php)
<HTML>
<TITLE>PHPBuddy getting screen resolution</TITLE>
<!--(c) http://www.phpbuddy.com (Feel free to use this script but keep this message intact)Author: Ranjit Kumar (Cheif Editor phpbuddy.com)-->
<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>
http://www.phpbuddy.com/icon/exclaim.gifEditor: PHPbuddy also uses the same kind of code to send optimized content for different resolutions if you are at 800*600 or lower resolution set your resolution to 1024*768 and check PHPbuddy! (Be sure to reload the site after you change your resolution)
http://www.phpbuddy.com/article.php?id=8
http://www.phpbuddy.com/images/dot.gif
Overview: In this article we will discuss how to get the users screen resolution. Wouldn’t it be great if you get the users screen resolution so that you could send optimized content for that particluar resolution, we can get users resolution using JavaScript’s but the problem is how do we get PHP to recognize it.
What we can do is get the users resolution using JavaScript and then set a cookie which has the users resolution, we can then access that cookie in PHP!
How can I get the users screen resoultion using JavaScript?
It's really simple I will show you the code
screen.width; //gives you the screen width
screen.height; //gives you the screen height
I will explain you the stuff with an example We will be using get_resolution.php with the help of JavaScript we will get the screen resolution and then set the screen resolution's value in a cookie named users_resolution when ever we execute the page we check with PHP to see if the cookie users_resolution is set, if it is not set we set it using JavaScript.
get_resolution.phphttp://www.phpbuddy.com/icon/run.gifView Sample Output (http://www.phpbuddy.com/sample/get_resolution.php)
<HTML>
<TITLE>PHPBuddy getting screen resolution</TITLE>
<!--(c) http://www.phpbuddy.com (Feel free to use this script but keep this message intact)Author: Ranjit Kumar (Cheif Editor phpbuddy.com)-->
<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>
http://www.phpbuddy.com/icon/exclaim.gifEditor: PHPbuddy also uses the same kind of code to send optimized content for different resolutions if you are at 800*600 or lower resolution set your resolution to 1024*768 and check PHPbuddy! (Be sure to reload the site after you change your resolution)
http://www.phpbuddy.com/article.php?id=8