在php.ini中error_reporting = E_ALL的情况下,输出内容之后再setcookie会弹出以下提示:
复制代码 代码如下:
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\b.php:2) in … on line …
但因为当时php.ini设置成了error_reporting = E_ALL & ~E_NOTICE,于是没有任何提示,所以开发的时候还是建议设成error_reporting = E_ALL 方便观察到一些异常情况。
附一个setcookie用法实例
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>