php 无法发送会话 cookie - 标头已发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4523363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:18:25  来源:igfitidea点击:

Cannot send session cookie - headers already sent

phpheader

提问by Andrej

Possible Duplicate:
Headers already sent by PHP

可能重复:
PHP 已发送的标头

Below is a simple example of my PHP code which (I hope so) is self explanatory. What I try to do is to update the session variable. But the output of the script is as follows:

下面是我的 PHP 代码的一个简单示例(我希望如此)是不言自明的。我尝试做的是更新会话变量。但是脚本的输出如下:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Library/WebServer/Documents/facebook/test.php:8) in /Library/WebServer/Documents/facebook/test.php on line 11

警告:session_start() [function.session-start]:无法发送会话 cookie - 头文件已由 /Library/WebServer/Documents/facebook 中的(输出开始于 /Library/WebServer/Documents/facebook/test.php:8)发送/test.php 第 11 行

The warning is caused by the echostatements in line 8 and 9, of course. Is there any simple solution to stop this warning.

当然,警告是由第echo8 行和第 9 行中的语句引起的。是否有任何简单的解决方案来阻止此警告。

Thanks for any pointers, Andrej

感谢您的指点,安德烈

<?php
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;

for ($i = 0; $i <= iterations; $i++) {
  $percent = ($i / $iterations) * 100;
  echo "Hello World!";
  echo "<br />";
  // update session variable
  session_start();
  $_SESSION['percent'] = number_format($percent, 0, '', '');
  session_commit();
}
?>

The only solution that works (i.e. updates the session variable) for me is:

对我来说唯一有效的解决方案(即更新会话变量)是:

<?php
ob_start();
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;

for ($i = 0; $i <= 50; $i++) {
  $percent = ($i / $iterations) * 100;
  echo "Hello World!";
  echo "<br />";
  // update session variable
  session_start();
  $_SESSION['percent'] = number_format($percent, 0, '', '');
  session_commit();
}
ob_flush();
?>

It's ugly, while it buffers the output first...

这很丑陋,而它首先缓冲输出......

采纳答案by b7kich

It's not possible to set cookies (or send any other headers) after output is started. You could add ob_start() at line 1 to buffer the output.

输出开始后无法设置 cookie(或发送任何其他标头)。您可以在第 1 行添加 ob_start() 以缓冲输出。

The right solution is to separate logic from the output. Check out e.g. http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

正确的解决方案是将逻辑与输出分开。查看例如 http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

回答by James

Remove the session_start() from inside the for loop.

从 for 循环内部删除 session_start()。

Put the session_commit() outside the for loop at the very end.

最后将 session_commit() 放在 for 循环之外。

Both these functions should only be called once in a script.

这两个函数只能在脚本中调用一次。

回答by dudi

If you don't have any previeus output before the session-start() statement,then try to resave your .php file as an ansi-file or as utf-8 without BOM file. that's bc in some cases the editor itself writes things as an output in the file.

如果您在 session-start() 语句之前没有任何 previeus 输出,那么尝试将您的 .php 文件重新保存为 ansi-file 或 utf-8 没有 BOM 文件。那是 bc 在某些情况下,编辑器本身将内容作为输出写入文件。

It worked for me

它对我有用

回答by Sarah

As the others have stated, the cause of the error is the second session_start()you are using. However, the actual reason it's throwing an error is because you are trying to set a header after you've already sent output. Since the session_start() function sets the session cookie, it tried to set the cookie header, which is after you already echo content.

正如其他人所说,错误的原因是session_start()您使用的第二个。但是,它引发错误的实际原因是因为您在已经发送输出后尝试设置标头。由于 session_start() 函数设置会话 cookie,它尝试设置 cookie 标头,这是在您已经回显内容之后。

回答by svens

You have to execute the session_start()function only once. So just drop the one within the loop.

您只需执行session_start()一次该功能。因此,只需将循环中的一个删除即可。

Also you don't have to do the session_commit()manually, in most cases PHP handles this for you automatically.

此外,您不必session_commit()手动执行此操作,在大多数情况下,PHP 会自动为您处理。