PHP setcookie Set Cookie usage (and the problem of invalid settings)_PHP tutorial

WBOY
Release: 2016-07-21 15:26:55
Original
1058 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:

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

www.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...
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