Jump to content


Photo

stop loop with cookies


  • Please log in to reply
7 replies to this topic

#1 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 24 April 2006 - 07:32 PM

I have set up this script to change the size of an image on our website, the size of the image depends on the users screen resolution width. The script first looks to see if there is a cookie set, if not it runs this java script. The javascript detects the screen resolution, sets a cookie, and then refreshes the page. It sets a cookie so the page does not have to refresh every time the user visits the page.

<?
if(isset($HTTP_COOKIE_VARS["users_resolution"]))
        $screen_res = $HTTP_COOKIE_VARS["users_resolution"];
else //means cookie is not found set it using Javascript
{
?>
<script language="javascript">
<!--
writeCookie();

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_resolution="+ screen.width;
 var the_cookie = the_cookie + ";expires=" + the_cookie_date;
 document.cookie=the_cookie

 location = '/';
}
//-->
</script>
<?
}



Some Code....


//Script that takes the users screen width and displays image accordingly
if ($screen_res < 1024)//If the screen width is less than 1024 this will shrink the image
{
  echo "<center><img width=300 src=\"random/rotator.php\"></center>";
} else {

//This is for screen widths greater than 1024
  echo "<center><img src=\"random/rotator.php\"></center>";

 }
?>

My problem is, if a user has cookies disabled, the page just keeps refreshing. Is there a way where if it refreshes three times it will set
$screen_res = 1024

Thanks
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#2 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 24 April 2006 - 07:47 PM

Theres some things that need fixed.

<?
if(isset($HTTP_COOKIE_VARS["users_resolution"]))
        $screen_res = $HTTP_COOKIE_VARS["users_resolution"];
else //means cookie is not found set it using Javascript
{
?>

Your leaving an open bracket. If you want the javascript code to be executed then you should do...

<?
if(isset($HTTP_COOKIE_VARS["users_resolution"])){
        $screen_res = $HTTP_COOKIE_VARS["users_resolution"];}
else //means cookie is not found set it using Javascript
{
echo '
<script language="javascript">
<!--
writeCookie();

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_resolution="+ screen.width;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = "/";
}
//-->
</script>';
}
?>

ect...



#3 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 24 April 2006 - 07:49 PM

Sorry, I have all the brackets correct in my web page, I missed it when I copied it to here
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#4 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 24 April 2006 - 08:05 PM

It still doesnt work? Try $_COOKIE['screen res'];

#5 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 24 April 2006 - 08:17 PM

[!--quoteo(post=368131:date=Apr 24 2006, 04:05 PM:name=rab)--][div class=\'quotetop\']QUOTE(rab @ Apr 24 2006, 04:05 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It still doesnt work? Try $_COOKIE['screen res'];
[/quote]

Yes it works, if the user has cookies enabled, it works great. But if the user has cookies disabled, it keeps trying to find a cookie when there isn't one, and if there isn't a cookie, then it tries to set one, but if the user has cookies disabled then it can't set a cookie, then the script keeps repeating its self. Like an infinite loop

I need a way to make it so when the user has cookies disabled, it will set $screen_res to 1024. I don't know if there is a script to detect that, or a script to make it only loop 3 times and then just set $screen_res to 1024.
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#6 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 24 April 2006 - 09:14 PM

run it through a loop then when you go to check it again, if the cookie is set, use that screen res. If not use a default or just load the certin image everytime

#7 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 25 April 2006 - 06:38 PM

How would I go about setting a loop up instead of if and else statements

Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#8 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 25 April 2006 - 07:24 PM

<?

function check_res()
{
    if(isset($HTTP_COOKIE_VARS["users_resolution"])) // checks 
    { 
        return 0;
    }else{ // if not found then setting it
       echo '
<script language="javascript">
<!--
writeCookie();

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_resolution="+ screen.width;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = "/";
}
//-->
</script>';
        if(isset($HTTP_COOKIE_VARS["users_resolution"])) // checking if it was set
        { 
            return 0;
        }else{
            return 1;
        }
     }
return 1;
}

if(check_res() == 0)
{
    $screen_res = $HTTP_COOKIE_VARS["users_resolution"];
}else{
//use default
}
?>

Not the best thing, but its something i whipped up in 2 minutes




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users