SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advnace a cookie value by one. Wut?

    I'm trying to set up a cookie that, for each visit to a page, counts to 12, and then starts over at one again.

    Code:
     // if there is no cookie, set one or if it's greater than 12, set it back to one since there's only 12 images
     if(!isset($_COOKIE['c_idximg']) || ($_COOKIE['c_idximg'] > 12)) { 
         setcookie("c_idximg",1, time()+86400*7); }
     else {
          // next page visit, it already exist, so advance it by one
    	    $imgNUM = $_COOKIE['c_idximg'];
            $imgNUM = $imgNUM + 1; // advance by one
            setcookie("c_idximg",$imgNUM, time()+86400*7); 
      }
    
    // output the values to see what they are!!
       echo $imgNUM.' this is imgNUM<br>';
       echo $_COOKIE["c_idximg"].' this is the cookie'; // this is ONE MORE than "imgNUM". WTH??
    What's freaking me out here, when I output the two values as above:
    1. "$imgNUM" is always one value higher than the cookie value.
    2. The cookie will count to 13 before resetting to one.

    Uhhh, I'm probably an idiot, but what's going on there that I don't get?

    edit: obviously I have no idea how to spell "Advnace" ...

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found another way to do it. Still puzzled by the first attempt above though.
    This works:

    Code:
     $idxIMG = 1;
     if (isset($_COOKIE["idxIMG"])) {
        $idxIMG = (int)$_COOKIE["idxIMG"];
    	   if($idxIMG > 12){
    		   $idxIMG = 1;
    		   setcookie("idxIMG", 1, time() + (60 * 60 * 24 * 30)); 
    	   }
     }		
     setcookie("idxIMG", $idxIMG + 1, time() + (60 * 60 * 24 * 30));


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •