
|
Added on 02.08.09 |
| Clicks 21755 |
| Rating: 2.00 out of 5 from 2 raters |
See a working example
Download
Working Code Now
This file has been downloaded 297 times.
The only way to grab users screen resolution is via Javascript and there
is not way to get it using only PHP as far as I know!!
Well,nothing new,but the trick is interacting
between the 2 programming languages and get the job done. It seems quite
easy just checking whether the cookie has been set and ,if not found, set it
using Javascript and then refreshing the page and retrieve it with PHP,but
what is going to happen if the user has Cookies disabled ? The code would
start an infinite loop refreshing the page over and over again because ,
obviously the cookie can't be set.To avoid all this,the trick is starting
the Session and check a value stored in a Session variable and preserved
when refreshing the page,if this value is set it is the second loop and
therefore we can go on reading the cookie whether it is set or not. Let us see the code:
First row,we start Session session_start()
then we check if $_SESSION[iterate] has been set,if not we check whether the
cookie is set if(!isset($HTTP_COOKIE_VARS["users_resolution2"]))
if cookie is not found set $SESSION[iterate]=1
and the cookie using Javascript <script language="javascript">
<!--
writeCookie();
function writeCookie()
{
var the_cookie = "users_resolution2="+ screen.width +"x"+ screen.height;
document.cookie=the_cookie
and then refresh the page
location ='<?php echo $_SERVER['PHP_SELF'];?>';
Refreshing the page,this time $_SESSION[iterate] has been set,and the code goes straight to retrieve the value from the cookie and if it's empty or unset it won't cause an infinite loop .
Here the code,you can copy and paste it for a test.The name of the file is get_resolution.php
Keep reading the next page