LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > How to get screen resolution
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     1782
Rating:     out of 5 from   0   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 2

 



Download Working Code Now

This file has been downloaded 208 times.

All is working fine if you can start a SESSION,but if ,for some reason, this script is included in another page and runs after any html ,it won't work because a SESSION can't be started after any html or even white space has been sent to the header.
However,to achieve the same result we can try this other method, that is a little more complicated but fast as well .Basically,instead of using SESSION to bring the value of $iterate  across different calls,we save it in a file and reload it after the second call.Let us see how:
include 'class/value_saver.php';
$d= dirname(__FILE__);

$saver=new value_saver($d);

$iterate=$saver->get();
if ($iterate!= 1)

First line we include the class that does all the file save and retrieving job,we'll talk about that later,then we find the root directory with _FILE_ and we open a new instance of the class.Then,using the class method ,get ,we load the value of $iterate from the file and we check it if is not equal to 1,we check whether the cookie is set if cookie is not found set the variable $iterate = 1 and save it to the file,then set the cookie with Javascript and reload the page
if(!isset($HTTP_COOKIE_VARS["users_resolution2"]))

//means cookie is not found set it using Javascript
{
$iterate=1;
$saver->save($iterate);
?>
<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>

When the page reloads the code will read the cookie and the loop will stop whether the cookie has been set or not.

Here the full script that you can copy and paste including the class value_saver.php

 

<?php
include 'class/value_saver.php';
$d= dirname(__FILE__);

$saver=new value_saver($d);

$iterate=$saver->get();
if ($iterate!= 1)
{
if(!isset($HTTP_COOKIE_VARS["users_resolution2"]))

//means cookie is not found set it using Javascript
{
$iterate=1;
$saver->save($iterate);
?>
<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{
$iterate=0;
$saver->save($iterate);
$screen_resolution = $HTTP_COOKIE_VARS["users_resolution2"];
}
echo $screen_resolution;
?>


File Name : value_saver.php


<?php
class value_saver{

var $fileDir;
function value_saver($filedir){


$this->fileDir=$filedir;
}
// save value to file
function save($data){
if(!$fp=fopen($this->fileDir.'/conf/data.txt','w')){
trigger_error('Error opening data
file',E_USER_ERROR);
}

fwrite($fp,$data);
fclose($fp);
}
// get value from file
function get(){

$contents= file_get_contents($this->fileDir.'/conf/data.txt');
return $contents;
}
}
?>

You can look at a working example here

 

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

i