Correct way to set/remove cookie?

Discussion in 'PHP' started by Tony Brar, Jun 25, 2012.

  1. #1
    Hi everyone,
    I just want to know, would the following code work for setting a raw array cookie:

    
    setrawcookie("userinfo[username]", $username, time()+60*60*24*21, "/", TRUE, TRUE);
    setrawcookie("userinfo[password]", $password, time()+60*60*24*21, "/", TRUE, TRUE);
    
    PHP:
    and this code for removing it?

    if (isset($_COOKIE["userinfo"]))
    {
    setrawcookie("userinfo[username]", $username, time()-60, "/", TRUE, TRUE);
    setrawcookie("userinfo[password]", $password, time()-60, "/", TRUE, TRUE);
    }
    
    PHP:
    Thanks in advance.
     
    Solved! View solution.
    Tony Brar, Jun 25, 2012 IP
  2. #2
    Everything looks good. On a side note, unsetting the cookie by calling a past date is correct, but I would use/set a blank value "" instead of the variables you're using.
     
    DevSwift, Jun 25, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    so like this for setting it

    
    setrawcookie("userinfo[username]", $username, time()+60*60*24*21, "/", TRUE, TRUE);
    setrawcookie("userinfo[password]", $password, time()+60*60*24*21, "/", TRUE, TRUE);
    PHP:
    but like this for removing it?

    if (isset($_COOKIE["userinfo"]))
    {
    setrawcookie("userinfo[username]", "", time()-60);
    setrawcookie("userinfo[password]", "", time()-60);
    }
    PHP:
     
    Tony Brar, Jun 25, 2012 IP
  4. Irfi0009

    Irfi0009 Banned

    Messages:
    17,584
    Likes Received:
    33
    Best Answers:
    1
    Trophy Points:
    48
    #4
    not removing all is good
     
    Irfi0009, Jun 25, 2012 IP