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>