Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/
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]
Link to comment
https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30237
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.
Link to comment
https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30254
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
Link to comment
https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30697
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.