A new php user asks about cookie

Source: Internet
Author: User
The code is as follows: {code...} why always outputs NULL? Why not output the set & #039; myvalue & #039 ;? The code is as follows:

Function cookie ($ name) {return isset ($ _ COOKIE [$ name])? $ _ COOKIE [$ name]: null;}/*** delete a Cookie from the client browser. * @ Param string $ cookie_name name of the Cookie to be deleted. * @ Return void */function delete_cookie ($ cookie_name) {setcookie ($ cookie_name, '', time ()-2*24*3600 ); unset ($ _ COOKIE [$ cookie_name]);} delete_cookie ('mycookier'); setcookie ('mycookier', 'myvalue'); var_dump (cookie ('mycookier ')); // why is NULL always output here? Why not output the set 'myvalue '?

Why always outputs NULL? Why not output the set 'myvalue '?

Reply content:

The code is as follows:

Function cookie ($ name) {return isset ($ _ COOKIE [$ name])? $ _ COOKIE [$ name]: null;}/*** delete a Cookie from the client browser. * @ Param string $ cookie_name name of the Cookie to be deleted. * @ Return void */function delete_cookie ($ cookie_name) {setcookie ($ cookie_name, '', time ()-2*24*3600 ); unset ($ _ COOKIE [$ cookie_name]);} delete_cookie ('mycookier'); setcookie ('mycookier', 'myvalue'); var_dump (cookie ('mycookier ')); // why is NULL always output here? Why not output the set 'myvalue '?

Why always outputs NULL? Why not output the set 'myvalue '?

COOKIE is taken from your client. Therefore, the COOKIE is set in the previous script. However, every time you execute this script, you delete the COOKIE you set last time, then a new COOKIE is set, which can only be read by the next running script. Then, we will continue the cycle of "Delete-set-delete-set... So it cannot be read.
If you change the COOKIE to a SESSION, the SESSION will not be as slow as the COOKIE. It is stored on the server.

The COOKIE value assignment takes effect only when the next browser request is sent. However, when you wait for the next browsing, you will delete it, so we will continue to hate this COOKIE ..

You have deleted $ _ COOKIE ['mycookier'] in this request, and setcookie takes effect in the next request.
$ _ COOKIE: the browser sends the cookie information to PHP.
Setcookie is used by PHP to tell the browser the cookie information.
That is, the $ _ COOKIE ['XXX'] generated requires the location of the setcookie ('XXX ')RequestExecution completed first.

If you want to output 'myvalue', you canunset($_COOKIE[$cookie_name]);Comment out. You can output

Unset takes effect immediately, but set_cookie takes effect only after the next request.

Similarly, if the unset statement is deleted, the output result is not just set_cookie, but the last set_cookie.

For example, if I delete the unset statement and refresh itstring(7) "myvalue", No problem;

Then I change myvalue to myvalue2, refresh, or displaystring(7) "myvalue";

Refresh again to displaystring(8) "myvalue2".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.