I'm having some issues with setcookie() not updating a cookie.

I have four links that each should change the value or set the value initially of a cookie called sort.

The first time I click a link, the script executes,

setcookie("sort","name_asc",time()+86400);

The value could be one of four things, name_asc, name_desc, filesize_asc and filesize_desc.

If I click the link for filesize_asc, it runs,

setcookie("sort","filesize_asc",time()+86400);

which should change the value of the cookie "sort", but it's not. Once the value gets set, PHP won't change it.

Any ideas on this one? Thanks for your help.

    Use the $_COOKIE superglobal array to change the cookie value once it's been set.

      I just tried the superglobal and it won't set the cookie either.

      One thing that is interesting is I tried setcookie("sort","",time()+86400) and that set the cookie (or actually removed it).

      I tried that because when you click logout in my application, it sets the cookie with the command setcookie("sessid","",1) which removes the cookie sessid.

        Okay, here's a simple example, even though it's a bad one. I think I'm on to the problem, although I'm not sure what to do. Once the cookie is set, and the redirect goes back to itself, it seems like the extract($_REQUEST) is still keeping the sort value.

        Note, this script goes into an infinite loop after it sets the cookie.

        You can see the basic idea though. The link sends the parameter $sort which gets extracted. If $sort is name_asc or name_desc, the cookie is set, and the script redirects back to itself. I was assuming the $sort parameter wouldn't get extracted if it wasn't sent, but if you uncomment the print_r($_REQUEST) line and refresh the page, you'll see that $sort is still in the array.

        <?php
        extract($_REQUEST);
        
        #print_r($_REQUEST); die();
        
        $phpself = $_SERVER['PHP_SELF'];
        
        session_start();
        
        if($sort == 'name_asc' || $sort == 'name_desc')
        {
                setcookie("sort",$sort,time()+86400);
                header("Location:{$phpself}"); exit();
        }
        
        $html = <<<eof
        <html>
        <head>
        <title>test cookie</title>
        </head>
        <body>
        <a href="{$phpself}?sort=name_asc">name_asc</a> | <a href="{$phpself}?sort=name_desc">name_desc</a>
        </body>
        </html>
        eof;
        
        echo($html);
        ?>
        

        Let me know what you think. If this example isn't good enough, I can send you the application I'm working on, but I'd rather not post it here or anywhere until it's done. Thanks.

          wsams wrote:

          Once the cookie is set, and the redirect goes back to itself, it seems like the extract($REQUEST) is still keeping the sort value.

          That would be because the $REQUEST array contains all request variables, both GET and COOKIE values. As the php.ini file says:

          ; This directive describes the order in which PHP registers GET, POST, Cookie,
          ; Environment and Built-in variables (G, P, C, E & S respectively, often
          ; referred to as EGPCS or GPC). Registration is done from left to right, newer
          ; values override older values.
          variables_order = "GPCS"

          In other words, you're never getting the $_GET value of the variable once the cookie is set since it overrides the value (same name).

          This is one example why you should never use [man]extract/man on $REQUEST - this simply imitates the register_globals directive which has been deprecated for some time. Get rid of the [man]extract/man call and don't use the $REQUEST array - use either $GET or $COOKIE, whichever is applicable.

          wsams wrote:

          Note, this script goes into an infinite loop after it sets the cookie.

          Right... that's expected:

          if($sort == 'name_asc' || $sort == 'name_desc')
          {
                  setcookie("sort",$sort,time()+86400);
                  header("Location:{$phpself}"); exit();
          }

          $sort will always be one or the other after it's been set via one of the links, so this if() statement will always evaluate to true.

            Thanks a lot, that makes sense. I didn't realize COOKIES were stored in the $_REQUEST array.

            I haven't tested to see if this helps, but I'm sure it will solve my problem, so I'm marking this resolved.

              Write a Reply...