Does anyone know a PHP script for getting a users screen resolution?
[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.
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?
sure, but his solution still works.
...
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript">
$().ready(function() {
$.get("/res.php", { x: screen.width , y: screen.height });
});
</script>
...
<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... )
<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>
<!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>