首页 文章

我无法setcookie“ Headers 已发送”[重复]

提问于
浏览
-2

可能重复:“警告:无法修改 Headers 信息 - Headers 已由”错误setcookie发送,无法修改 Headers 信息 - Headers 已发送

我有这个代码来设置cookie:

if (!isset($_COOKIE["CorkIU"])) {
setcookie("CorkIU", 2dd2ee3aUgsvoRye, time()+60*60*24*365, "/");
}

我在layout.php文件中添加了此代码

layout.php文件的第一行是

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<?
    include ("inc/css.php");

if (!isset($_COOKIE["CorkIU"])) {
setcookie("CorkIU", 2dd2ee3aUgsvoRye, time()+60*60*24*365, "/");
}

?>
<head>
<title>Welcome To Example</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="style/default.css" rel="stylesheet" type="text/css">
</head>

这是错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/ex/public_html/template/layout.php:4) in /home/ex/public_html/template/layout.php on line 8

2 回答

  • 3

    该错误告诉您需要知道的一切:

    Headers 已经发送(输出

    在输出任何内容之前将调用移至 setcookie (您在此处执行: <!DOCTYPE html

  • 0

    来自 PHP manual

    setcookie()定义了一个与其他HTTP头一起发送的cookie . 与其他标头一样,必须在脚本输出之前发送cookie(这是协议限制) . 这要求您在任何输出之前调用此函数,包括和标记以及任何空格 .

相关问题