Jump to content

Archived

This topic is now archived and is closed to further replies.

theycallmepj

stop loop with cookies

Recommended Posts

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.

[code]
<?
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>";

}
?>
[/code]

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 [code]$screen_res = 1024[/code]

Thanks

Share this post


Link to post
Share on other sites
Theres some things that need fixed.

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

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

[code]
<?
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...

[/code]

Share this post


Link to post
Share on other sites
It still doesnt work? Try $_COOKIE['screen res'];

Share this post


Link to post
Share on other sites
[!--quoteo(post=368131:date=Apr 24 2006, 04:05 PM:name=rab)--][div class=\'quotetop\']QUOTE(rab @ Apr 24 2006, 04:05 PM) [snapback]368131[/snapback][/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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]
<?

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
}
?>
[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.