php $_COOKIE isset

后端 未结 3 1555
温柔的废话
温柔的废话 2020-12-20 10:24

I am using this code to set a cookie and then see if they exist

setcookie(\"token\", \"value\", time()+60*60*24*100, \"/\");
setcookie(\"secret\", \"value\",         


        
相关标签:
3条回答
  • 2020-12-20 11:11

    as my testing,we can't use cookies in same time.if you set cookies. you need to reload page to grab those. put like this

    if (!isset($_COOKIE['token'])) {
        setcookie("token", "value", time()+60*60*24*100, "/"); //this set cookies for first time
    }
    
    0 讨论(0)
  • 2020-12-20 11:16

    use

    if(true === array_key_exists('secret', $_COOKIE) && strlen($_COOKIE['secret']) > 0) {
    }
    
    0 讨论(0)
  • 2020-12-20 11:28

    setcookie only defines a cookie to be sent along with the rest of the HTTP headers, and they can be accessed on the next page load with the $_COOKIE. With your code, the HTTP headers are not be sent.

    You just need setcookie when a cookie is not set. Like:

    if (!isset($_COOKIE['token'])) {
        setcookie("token", "value", time()+60*60*24*100, "/");
    }
    
    0 讨论(0)
提交回复
热议问题