无法发送会话Cookie - 已发送的标头 [英] Cannot send session cookie - headers already sent

查看:146
本文介绍了无法发送会话Cookie - 已发送的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

已由PHP发送的头文件


下面是一个简单的例子我的PHP代码(我希望如此)是自我解释。我试图做的是更新会话变量。但是脚本的输出如下:

lockquote
警告:session_start()
[function.session-start]:不能发送
会话cookie - 头文件已经发送
(输出开始于
/Library/WebServer/Documents/facebook/test.php:8)
in
/ Library /WebServer/Documents/facebook/test.php第11行

警告是由 echo 语句当然是在第8行和第9行。是否有任何简单的解决方案来阻止此警告。



感谢任何指针,Andrej

 <?php 
session_start();
$ _SESSION ['percent'] = 0;
$ iterations = 50; ($ i = 0; $ i <= iterations; $ i ++){
$ percent =($ i / $ iterations)* 100;

;
回声Hello World!;
回声< br />;
//更新会话变量
session_start();
$ _SESSION ['percent'] = number_format($ percent,0,'','');
session_commit();
}
?>

对我而言,唯一可行的解​​决方案是(即更新会话变量):

 <?php 
ob_start();
session_start();
$ _SESSION ['percent'] = 0;
$ iterations = 50; ($ i = 0; $ i <= 50; $ i ++){
$ percent =($ i / $ iterations)* 100;

;
回声Hello World!;
回声< br />;
//更新会话变量
session_start();
$ _SESSION ['percent'] = number_format($ percent,0,'','');
session_commit();
}
ob_flush();
?>

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

解决方案

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

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


Possible Duplicate:
Headers already sent by 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:

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

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

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

解决方案

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.

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

这篇关于无法发送会话Cookie - 已发送的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆