LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > How to get screen resolution P1
Useful

Links


Free technical Tutorials At techtutorials.net



Link Exchange at iWEBTOOL.com

 



The PHP Resource Index

iBuyOfficesupply.com Inc. There is not Comments for this Article,Be the first one
Ask any relevant question in the Comments or leave your valuable feed-back

printer  Print version of this article

star  Add it to your favourite!

mail email this page to a frend
LinkShare  Referral  Program
Added on   02.08.09
Clicks     21755
Rating:   2.00   out of 5 from   2   raters      
         rate this page   
 Not rated yet.Be the first one to vote!!

How to get Users Screen Resolution with PHP and Javascript



 Get Screen Resolution with PHP - Javascript Pag 1

 



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

<<?php
session_start();

if (!isset($_SESSION[iterate]))
{
if(!isset($HTTP_COOKIE_VARS["users_resolution2"]))

//means cookie is not found set it using Javascript
{
$_SESSION[iterate]=1;
?>
<script language="javascript">
<!--
writeCookie();

function writeCookie()
{

var the_cookie = "users_resolution2="+ screen.width +"x"+ screen.height;

document.cookie=the_cookie

location ='<?php echo $_SERVER['PHP_SELF'];?>';
}
//-->
</script>
<?php
}
else{
$screen_resolution = $HTTP_COOKIE_VARS["users_resolution2"];
}
}
else{
$screen_resolution = $HTTP_COOKIE_VARS["users_resolution2"];
}
echo $screen_resolution;
?>

Keep reading the next page

Comments

Post your comment here or ask any question that is rilevant on what is shown in this page.
Thank you for visiting this website

There is not comments for this page yet.Be the first to post one   add a comment