As a result, I encountered a problem. The cookie set by setcookie did not take effect, and I did not see it on the browser side. After checking, it turns out that setcookie is completed through the header of the HTTP request response 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, after outputting the content and then setting cookie, the following prompt will pop up:
Copy code The code is as follows :
Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsb.php:2) in … on line …
But because at that time php.ini is set to error_reporting = E_ALL & ~E_NOTICE, so there is no prompt. Therefore, it is recommended to set error_reporting = E_ALL during development to facilitate observation of some abnormal situations.
Attached is a setcookie usage example
a.php
Copy code The code is as follows:
< ;?php
setcookie("page", "a.php");
$page = $_COOKIE["page"] ? $_COOKIE["page"] : "unknown";
echo " From " . $page . "
";
?>
This is a.php. Go to
b.php
b.php
Copy code The code is as follows:
setcookie("page", "b.php");
$page = isset($_COOKIE["page"]) ? $_COOKIE["page"] : "unknown";
echo "From " . $page . "
";
?>
This is b.php. Go to
a.php
http://www.bkjia.com/PHPjc/323842.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323842.htmlTechArticleAs a result, I encountered a problem. The cookie set by setcookie did not take effect, and I did not see it on the browser side. After checking, it turns out that setcookie is done through the header of the HTTP request response...