Accessing $_COOKIE immediately after setcookie()

前端 未结 9 1055
轻奢々
轻奢々 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: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);  
    }
    

提交回复
热议问题