This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.


 

Dodo's Scripts Collection


Basic TutorialsBasic Tutorials

How to get user screen's width and height with PHP?
I've known there's a way to get user's screen resolution with PHP for a while but never played with it until today. The link posted does a good job but I wanted something more.

If you do not wish to learn about the detailed background about the script, simply skip to how to install the script for your use.

PHP can not directly detect your screen resolution because it's ran remotely and does not have user side ability like javascript. Javascript runs with your browser so it can easily detect your resolution. You can see your resolution with javascript simply by using the following script:
<script>
document.write("Your resolution is "+screen.width+"x"+screen.height);
</script>
In order for your php to get those variables, there has to be some kind of connection between javascript and PHP. That connection is your browser cookie. So the javascript reads in the info and save it in your cookie then your PHP script reads the info from your cookie. That's exactly what this script is trying to accomplish. It does a very good job but it doesn't fulfill my need. Also I think there's a bug in the script. Because the way it's written, it would not detect the change in resolution before the cookie expires which is December 31, 2023. I modified the script to fix this bug and also added one more function to allow the file to be included in another PHP file. That greatly expands the usibilty of this script.

Here's a demonstration of my script. The actual get_resolution.php is actually included in the test_get_res.php file you are viewing.

Since get_resolution.php refreshes itself if it is detecting your resolution for the first time or if it finds that your resolution has changed from what's stored in your cookie, it is important to have the script redirect it back to the PHP script you include it in. get_resolution.php has to refresh because it takes TWO steps for the connection between javascript and php to work the first time. Step 1: javascript stores the cookie var Step 2: php grabs the cookie var. This can not be done without refreshing the script. But once your resolution is stored and the script does not detect a change in resolution, it will only need to do step 2. However, how does get_resolution.php know it's being included in test_get_res.php or some other php files? This is what $GLOBAL vars come in. You can learn more about it in this article. When you include get_resolution.php, you send the file name of the script that includes get_resolution.php as a $GLOBAL var and get_resolution.php will be able to read it. This is why you need the following code when you include:
<?php
$callget_res_page_name = $REQUEST_URI;
$GLOBALS[callget_res_page_name] = $callget_res_page_name;
include("get_resolution.php");
echo $screen_width." is your screen width and ".$screen_height." is your screen height.";
?>


How to install
Again here's a demonstration of my script. To install my script is easy. Simply grab my source code, copy and paste it to a text editor and save the file as get_resolution.php. Then upload to where you need and call it with:
<?php
$callget_res_page_name = $REQUEST_URI;
$GLOBALS[callget_res_page_name] = $callget_res_page_name;
include("get_resolution.php");
echo $screen_width." is your screen width and ".$screen_height." is your screen height.";
?>
Of course you might not want to simply echo the variables. Do whatever you want with them. THIS WILL NOT WORK! If you have loaded the script into your browser and then immediately change your resolution. After that if you refresh the script in your browser, it will NOT change the values for the variables. I do not know why. However, if you close your browser and open a new one, then load the script again, it will change the values for the variables. If you know a way to fix this bug, please let me know.

Back