Cookie settings PHP setcookie settings Cookie usage and invalid settings

PHP中文网
Release: 2023-02-28 21:28:02
Original
1834 people have browsed it

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 …
Copy after login

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>
Copy after login

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>
Copy after login

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?

PHP setcookie usage

php setcookie function invalid

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template