Home  >  Article  >  Backend Development  >  A newbie in php has a question about cookies

A newbie in php has a question about cookies

WBOY
WBOYOriginal
2016-10-10 11:56:17841browse

The code is as follows:

<code>function cookie($name) {
    return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
}

/**
 * 从客户端浏览器删除一个 Cookie。
 * @param  string $cookie_name 要删除的 Cookie 名称。
 * @return void
 */
function delete_cookie($cookie_name) {
    setcookie($cookie_name, '', time() - 2 * 24 * 3600);
    unset($_COOKIE[$cookie_name]);
}

delete_cookie('mycookie');
setcookie('mycookie', 'myvalue');
var_dump(cookie('mycookie')); // 为何这里总是输出NULL?为什么不是输出刚刚设置的'myvalue'?</code>

Why is NULL always output? Why isn't the 'myvalue' just set output?

Reply content:

The code is as follows:

<code>function cookie($name) {
    return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
}

/**
 * 从客户端浏览器删除一个 Cookie。
 * @param  string $cookie_name 要删除的 Cookie 名称。
 * @return void
 */
function delete_cookie($cookie_name) {
    setcookie($cookie_name, '', time() - 2 * 24 * 3600);
    unset($_COOKIE[$cookie_name]);
}

delete_cookie('mycookie');
setcookie('mycookie', 'myvalue');
var_dump(cookie('mycookie')); // 为何这里总是输出NULL?为什么不是输出刚刚设置的'myvalue'?</code>

Why is NULL always output? Why isn't the 'myvalue' just set output?

COOKIE is brought by you from your client, so the COOKIE read should be set by the last script. However, every time you execute this script, you delete the COOKIE set last time and set a new COOKIE. , this COOKIE can only be read by the script run next time. Then continue the cycle of delete-set-delete-set. . . So I can't read it.
If you change COOKIE to SESSION, you can read it. SESSION will not be as slow as COOKIE. It is saved on the server.

Because the assignment of COOKIE will not take effect until the next browsing request. But you delete it when you browse it next time, so I keep wondering how much hatred this COOKIE has for you...

You have deleted $_COOKIE['mycookie'] in this request, and setcookie needs the next request to take effect.
$_COOKIE is the browser passing the cookie information to PHP.
setcookie is PHP telling the browser the cookie information .
That is, the generation of $_COOKIE['xxx'] requires the requestwhere setcookie('xxx') is located to be executed first.

If you want to output ‘myvalue’, you can comment out unset($_COOKIE[$cookie_name]);. You can output it

unset will take effect immediately, but set_cookie will not take effect until the next request.

Similarly, if you delete the unset sentence, the output result will not be the one set_cookie just now, but the last set_cookie.

For example, if I delete the sentence unset and then refresh, it will display string(7) "myvalue", no problem;

Then I changed myvalue to myvalue2, then refreshed, and it still showed string(7) "myvalue"

Refresh again and string(8) "myvalue2" will be displayed.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn