Accessing $_COOKIE immediately after setcookie()

前端 未结 9 922
轻奢々
轻奢々 2020-11-22 05:43

I\'m trying to access a cookie\'s value (using $_COOKIE) immediately after calling the setcookie() function in PHP. When I do so, $_COOKIE[\

相关标签:
9条回答
  • 2020-11-22 05:54

    The cookie isn't set until the response is sent back to the client, and isn't available in your PHP until the next request from the client after that.

    However, when you set the cookie in your script, you can do:

    setcookie('uname', $uname, time()+60*30);
    $_COOKIE['uname'] = $uname;
    
    0 讨论(0)
  • 2020-11-22 05:56

    If you want to access a cookie's value immediately after calling the setcookie() you can't use $_COOKIE. The reason for this is in the nature of the protocol (see https://tools.ietf.org/html/rfc6265). When you use setcookie() it defines a Cookie to be sent along with the rest of the HTTP headers to the client (see http://php.net/manual/en/function.setcookie.php). But $_COOKIE on the other hand contains variables passed to the current script via HTTP Cookies from the client (http://php.net/manual/en/reserved.variables.cookies.php).

    When you change $_COOKIE after calling setcookie() - like some answers here recommend - it doesn't contain only the Cookies from the client any more. This could interferer with assumptions made in third party code used in your application and may result in unwanted site effects. So in general it's not good practice and it's only an option when the calls of setcookie() are part of your own code.

    A clean and transparent way to get a value set with setcookie() within the same request is to use headers_list() (see http://php.net/manual/en/function.headers-list.php):

    function getcookie($name) {
        $cookies = [];
        $headers = headers_list();
        // see http://tools.ietf.org/html/rfc6265#section-4.1.1
        foreach($headers as $header) {
            if (strpos($header, 'Set-Cookie: ') === 0) {
                $value = str_replace('&', urlencode('&'), substr($header, 12));
                parse_str(current(explode(';', $value, 1)), $pair);
                $cookies = array_merge_recursive($cookies, $pair);
            }
        }
        return $cookies[$name];
    }
    // [...]
    setcookie('uname', $uname, time() + 60 * 30);
    echo "Cookie value: " . getcookie('uname');
    

    But notice this won't work in PHP CLI (e.g. PHPUnit). In such a case you could use third party extensions like XDebug (see http://xdebug.org/docs/all_functions#xdebug_get_headers).

    0 讨论(0)
  • 2020-11-22 05:58

    You have to set the cookie variable by yourself if you need it immediately, by the time you load another page the real cookie would have been set as a result of the setcookie method.

    setcookie('name', $value, time()+60*30);
    $_COOKIE ['name'] = $value;
    
    0 讨论(0)
  • 2020-11-22 05:59

    $_COOKIE is set when the page loads, due to the stateless nature of the web. If you want immediate access, you can set $_COOKIE['uname'] yourself or use an intermediate variable.

    For example:

    if (isset($_COOKIE['uname'])) {
        // get data from cookie for local use
        $uname = $_COOKIE['uname'];
    }
    else {
        // set cookie, local $uname already set
        setcookie('uname', $uname, time() + 1800);  
    }
    
    0 讨论(0)
  • 2020-11-22 06:04

    Your script's setcookie() function runs when the web browser requests the page for the first time, in your case the reload. This cookie is stored in the users browser and isn't available to your script running on the server until the next request, or in your case the next reload.

    Upon the next request the browser sends that cookie to the server and the array $_COOKIE will have the value that you initially set and the browser sent back upon the second request.

    0 讨论(0)
  • 2020-11-22 06:12

    I had a similar problem where i used a function from a included file and solved it with a function that both returns the value of the cookie and sets the cookie.

    function setCookie($input) {
      setcookie('uname', $input, time() + 60 * 30);
      return $input;
    }
    
    if(!isset($_COOKIE['uname'])) {
        $uname  = setCookie($whatever);
    } else {
        $uname = $_COOKIE['uname'];
    }
    
    echo "Cookie value: " . $uname;
    
    0 讨论(0)
提交回复
热议问题