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;
$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(){
Brilliant - just what I have been looking for.
Works a treat when uploaded to my web server but when I run it on my test server I get a blank page. I am using apache (I used XAMPP to install it). Any suggestions as to why?