PHP setcookie setting Cookie usage (and invalid settings)

Source: Internet
Author: User

As a result, the setcookie setting does not take effect and is not displayed on the browser. After checking, it turns out that setcookie is implemented through the HTTP request response Header and needs to be executed before the request response content is output (just like other Header settings ).

In the case of error_reporting = E_ALL in php. ini, the following prompt is displayed when the output content is followed by setcookie:
Copy codeThe Code is as follows:
Warning: Cannot modify header information-headers already sent by (output started at C: \ xampp \ htdocs \ B. php: 2) in... On line...

But since php. ini was set to error_reporting = E_ALL &~ E_NOTICE, so there is no prompt, so it is recommended to set error_reporting = E_ALL during development to facilitate observation of some exceptions.

An example of setcookie usage is provided.
A. php
Copy codeThe Code is as follows:
<? Php
Setcookie ("page", "a. php ");
$ Page = $ _ COOKIE ["page"]? $ _ COOKIE ["page"]: "unknown ";
Echo "From". $ page. "<br/> ";
?>
This is a. php. Go to <a href = "B. php"> B. php </a>

B. php
Copy codeThe Code is as follows:
<? Php
Setcookie ("page", "B. php ");
$ Page = isset ($ _ COOKIE ["page"])? $ _ COOKIE ["page"]: "unknown ";
Echo "From". $ page. "<br/> ";
?>
This is B. php. Go to <a href = "a. php"> a. php </a>

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.