Home >Backend Development >PHP Tutorial >Cookie settings PHP setcookie settings Cookie usage and invalid settings
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:
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\b.php:2) in … on line …
But because php.ini was set to error_reporting = E_ALL & ~E_NOTICE at that time, there was no prompt, so this was developed It is still recommended to set error_reporting = E_ALL to facilitate observation of some abnormal situations.
Attached is an example of setcookie usage
a.php
<?php setcookie("page", "a.php"); $page = $_COOKIE["page"] ? $_COOKIE["page"] : "unknown"; echo "From " . $page . "<br /><br />"; ?> This is a.php. Go to <a href="b.php">b.php</a>
b.php
<?php setcookie("page", "b.php"); $page = isset($_COOKIE["page"]) ? $_COOKIE["page"] : "unknown"; echo "From " . $page . "<br /><br />"; ?> This is b.php. Go to <a href="a.php">a.php</a>
The above introduces the cookie settings PHP setcookie settings cookie usage and invalid settings, including cookie settings, I hope you are interested in PHP tutorials Friends help.
Related articles:
If setcookie does not set the expiration time, how should I write the set path?