PHP Script For Getting Users Screen Resolution?



Does anyone know a PHP script for getting a users screen resolution?

...and that will conclude this edition of Dumb Fucking Lazy Pricks Who Won't Search Google Before Asking A Question!

We thank you for playing and before we go, a quick word from one of our sponsors...

384531-ronaldmcgtfo_super.jpg


Thread-Sucks-Chick_suck.gif
 
What cracks me up is it took longer for him to write a fucking post here than it did for him to google it.

Cheshire
 
My #1 pet peave on the internet: Person A asks "How do you do XYZ?" and person B answers, "GOOGLE YOU FUCKWAD."

Then, three months later, when I search "XYZ" on Google, there's NO FUCKING ANSWER IN THIS THREAD.


OP, there's no way to do this through PHP, as PHP is a server side language, and resolution is a client side setting (the clients resolution is never passed in the HTTP request, which dictates the extent of client-side information PHP has available at runtime).
In other words, PHP renders the page before it ever makes it to your clients' browser, and the client never shares this information.

Your best alternative will be writing JavaScript to read the clients resolution, then finding an effective (== depends on what you want to do with it) way to pass this back to the server.
 
Code:
[FONT=verdana][SIZE=2][COLOR=#000000]<HTML> 
<TITLE>[B]php[/B]Buddy [B]get[/B]ting screen [B]resolution[/B]</TITLE> 
<!-- 
(c) [URL="http://www.webmasterworld.com/redirect.cgi?f=88&d=4700&url=http://www.phpbuddy.com"]http://www.[B]php[/B]buddy.com[/URL]  (Feel free to use this script but keep this message intact) 
Author: Ranjit Kumar (Cheif Editor [B]php[/B]buddy.com) 
--> 
<HEAD> 
<? 
if(isset($HTTP_COOKIE_VARS["users_[B]resolution[/B]"])) 
$screen_res = $HTTP_COOKIE_VARS["users_[B]resolution[/B]"]; 
else //means cookie is not found set it using Javascript 
{ 
?> 
<script language="javascript"> 
<!-- 
writeCookie(); [/COLOR][/SIZE][/FONT]
[FONT=verdana][SIZE=2][COLOR=#000000]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_[B]resolution[/B]="+ screen.width +"x"+ screen.height; 
 var the_cookie = the_cookie + ";expires=" + the_cookie_date; 
 document.cookie=the_cookie 
  
 location = '[B]get[/B]_[B]resolution[/B].[B]php[/B]'; 
} 
//--> 
</script> 
<? 
} 
?> 
</HEAD> 
<BODY> 
<?[B]php[/B] 
echo "Your Screen [B]resolution[/B] is set at ". $screen_res; 
?> 
</BODY> 
</HTML>      [/COLOR][/SIZE][/FONT]
 
Code:
[FONT=verdana][SIZE=2][COLOR=#000000]<HTML> 
<TITLE>[B]php[/B]Buddy [B]get[/B]ting screen [B]resolution[/B]</TITLE> 
<!-- 
(c) [URL="http://www.webmasterworld.com/redirect.cgi?f=88&d=4700&url=http://www.phpbuddy.com"]http://www.[B]php[/B]buddy.com[/URL]  (Feel free to use this script but keep this message intact) 
Author: Ranjit Kumar (Cheif Editor [B]php[/B]buddy.com) 
--> 
<HEAD> 
<? 
if(isset($HTTP_COOKIE_VARS["users_[B]resolution[/B]"])) 
$screen_res = $HTTP_COOKIE_VARS["users_[B]resolution[/B]"]; 
else //means cookie is not found set it using Javascript 
{ 
?> 
<script language="javascript"> 
<!-- 
writeCookie(); [/COLOR][/SIZE][/FONT]
[FONT=verdana][SIZE=2][COLOR=#000000]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_[B]resolution[/B]="+ screen.width +"x"+ screen.height; 
 var the_cookie = the_cookie + ";expires=" + the_cookie_date; 
 document.cookie=the_cookie 
  
 location = '[B]get[/B]_[B]resolution[/B].[B]php[/B]'; 
} 
//--> 
</script> 
<? 
} 
?> 
</HEAD> 
<BODY> 
<?[B]php[/B] 
echo "Your Screen [B]resolution[/B] is set at ". $screen_res; 
?> 
</BODY> 
</HTML>      [/COLOR][/SIZE][/FONT]

It'd be easier to just use something like jquery to do an ajax call behind the scene to the php file.
 
My #1 pet peave on the internet: Person A asks "How do you do XYZ?" and person B answers, "GOOGLE YOU FUCKWAD."

Then, three months later, when I search "XYZ" on Google, there's NO FUCKING ANSWER IN THIS THREAD.


OP, there's no way to do this through PHP, as PHP is a server side language, and resolution is a client side setting (the clients resolution is never passed in the HTTP request, which dictates the extent of client-side information PHP has available at runtime).
In other words, PHP renders the page before it ever makes it to your clients' browser, and the client never shares this information.

Your best alternative will be writing JavaScript to read the clients resolution, then finding an effective (== depends on what you want to do with it) way to pass this back to the server.

Well the first thing I did was punch it into google, and what do you know, the first few results ALL had the solution in it, so I'm going to flame a douche bag about it.

Sorry you can't seem to get help, bug the right people and they'll lead you to the answer.
 
Well the first thing I did was punch it into google, and what do you know, the first few results ALL had the solution in it, so I'm going to flame a douche bag about it.

Sorry you can't seem to get help, bug the right people and they'll lead you to the answer.

This has nothing to do with "I can't get help", and everything to do with "You responded like a smug cunt, called him a dipshit, and didn't answer his question, probably because that's the trendy thing to do around here."

My original post didn't even take a stab at you. I answered the guy's fucking question.

"Sorry you can't seem to get help"? Fuck yourself. I did not ask for help. I helped.
 
I am a newbie to PHP and JavaScript so this isn't a critique, but what is the above code really doing? As I see it, it checks the cookie, the JavaScript runs and resets the cookie regardless (is the else totally pointless there?), then it just echos the cookie variable to the screen. Why is the PHP even there?
 
This has nothing to do with "I can't get help", and everything to do with "You responded like a smug cunt, called him a dipshit, and didn't answer his question, probably because that's the trendy thing to do around here."

My original post didn't even take a stab at you. I answered the guy's fucking question.

"Sorry you can't seem to get help"? Fuck yourself. I did not ask for help. I helped.

I'm not sure why your taking this so personal you fuck wad. I'm saying the OP is a fucking retard who can't use google. Why in fucks name would you sit here and answer a question that the answer is easy to find (besides trying to look like a smart ass)! I pointed his ass in the right direction and he can eat shit, just like you can.

Take a fucking midol bitch.
 
You both suck for being off topic.

However I am clueless to JavaScript and I already came up with a better solution to the question, yes, by Googling.
 
I am a newbie to PHP and JavaScript so this isn't a critique, but what is the above code really doing? As I see it, it checks the cookie, the JavaScript runs and resets the cookie regardless (is the else totally pointless there?), then it just echos the cookie variable to the screen. Why is the PHP even there?


It checks to see if a certain cookie has been set. If it's been set it skips the else part and outputs the screen resolution. If it's not been set the javascript in the else part will set a cookie and then send the browser to the same script, where it will check for the cookie and will print the output if the cookie exists.

The code is old and buggy. $_COOKIE should be used instead of $HTTP_COOKIE_VARS and if cookies are disabled the browser will go into an infinite loop.

As KB suggested, these days you'd probably be better of using ajax.
 
sure, but his solution still works.

Example using Jquery to send the screen resolution to a php file that can stash it for statistics (he only said to use a PHP script for getting the resolution, nothing regarding changing the user's window size, so only gona concentrate on getting PHP the resolution)

Code:
...
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript">
	$().ready(function() {
		$.get("/res.php", { x: screen.width , y: screen.height });
	});
</script>
...

Course using JSON you could probably take a bunch of other information thats detectable by javascript and post them to a php, kinda like what google analytics does.
 
This is what I came up with; sets a cookie or passes to the URL. ( Testing screen resolution... )

Code:
<html>
<head>
<title>Testing screen resolution...</title>

<?php
if(!$_COOKIE['user_res'])
echo <<<ENDJS
<script language="JavaScript">
document.cookie = 'user_res=' + screen.width + 'x' + screen.height;
location = 'screenres.php?user_res=' + screen.width + 'x' + screen.height;
</script>
ENDJS
?>

</head>
<body>

<?php
echo 'Screen res: ', $_COOKIE['user_res'], ' (from cookie, PHP)<br><br>', "\n";
echo 'Screen res: ', $_GET['user_res'], ' (from URL, PHP)<br><br>', "\n";
?>

<script language="JavaScript">
document.write('Screen res: ' + document.cookie + ' (from cookie, JavaScript)<br><br>');
document.write('Screen res: ' + screen.width + 'x' + screen.height + ' (direct from JavaScript)');
</script>

</body>
</html>
 
This is what I came up with; sets a cookie or passes to the URL. ( Testing screen resolution... )

These changes will stop the infinite loop if cookies are disabled. But seriously. You most certainly want to use ajax. There are more lightweight ajax-only libraries out there(like this one) if you don't think it's worth loading jquery for.

Code:
<html>
<head>
<title>Testing screen resolution...</title>

<?php
if(!$_COOKIE['user_res'] && !isset($_GET['user_res']))
echo <<<ENDJS
<script language="JavaScript">
document.cookie = 'user_res=' + screen.width + 'x' + screen.height;
location = 'screenres.php?user_res=' + screen.width + 'x' + screen.height;
</script>
ENDJS
?>

</head>
<body>

<?php

if (isset($_COOKIE['user_res'])) {
  echo 'Screen res: ', $_COOKIE['user_res'], ' (from cookie, PHP)<br><br>', "\n";
  echo 'Screen res: ', $_GET['user_res'], ' (from URL, PHP)<br><br>', "\n";

?>
<script language="JavaScript">
document.write('Screen res: ' + document.cookie + ' (from cookie, JavaScript)<br><br>');
document.write('Screen res: ' + screen.width + 'x' + screen.height + ' (direct from JavaScript)');
</script>

<?php
}
else {

  echo "Not able to get data. Make sure cookies and javascript are enabled, dicko.";
}
?>



</body>
</html>
Or just add a line to KB's example and you can record the resolution and output it to the user as well using ajax:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Screen Res</TITLE>

  <script type="text/javascript" src="jquery.js"></script>

  <script type="text/javascript">

    $(document).ready(function(){

      $.get('save_res.php', {x: screen.width, y: screen.height});

      $('#screen-res').html('Your resolution is ' + screen.width + 'x' + screen.height);

    });

  </script>

 </HEAD>

 <BODY>
   <div id="screen-res"></div>
 </BODY>
</HTML>
 
Similar to this I was recently trying to find a way to get the total pixel height and width of a rendered web page. But I did not want to call something from command line and screencap and measure that. I forget now the ie and mozilla elements that contain this info but the problem was that the measurements were inconsistent.

For example of what I mean, imagine if you use a plug in or application like FireShot. You can capture "entire page" meaning it gives a screenshot of the entire page rendered. Of course this uses tools external to the browser, but does anyone know if it can be possible to measure the entire height and width of the rendered page as like this?