【问题标题】:Understanding cache limiter | headers already sent php warning了解缓存限制器 |标头已发送 php 警告
【发布时间】:2013-10-08 17:52:55
【问题描述】:

我有一个运行良好的网站,但只要刷新主页 (index_3.php),我的错误日志就会出现两个警告。

[08-Oct-2013 11:36:09] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home2/mysite/public_html/mysubsite/index_3.php:7) in /home2/mysite/public_html/mysubsite/functions.php on line 12

[08-Oct-2013 11:36:09] PHP Warning:  session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent in /home2/mysite/public_html/mysubsite/functions.php on line 13

我已经进行了足够的研究,以了解在会话开始之前某些东西正在发送页面数据,但我似乎无法根除原因。 functions.php 直接取自本教程站点的“创建 PHP 函数”。 http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

在 index_3.php 上加载的还有 get_opwire.php,它只是放置一个表格。 get_opwire.php 的开头是这样的:

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();

sec_session_start(); 是位于functions.php 中的自定义会话开始

当我尝试将 sec_session 的顺序重新排列到顶部或在桌子休息时移动它们时。 Index_3.php 只是主页,主要是包含提交表单和 get_opwire.php 的 html

有人能帮我指出问题吗?

编辑:index_3.php(从第 1 行到 )

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
 <meta name="Generator" content="Xara HTML filter v.6.0.1.335"/>
 <meta name="XAR Files" content="index_htm_files/xr_files.txt"/>
 <title>index_3</title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
 <link rel="stylesheet" type="text/css" href="index_htm_files/xr_main.css"/>
 <link rel="stylesheet" type="text/css" href="index_htm_files/xr_text.css"/>
 <link rel="stylesheet" type="text/css" href="index_htm_files/custom_styles.css"/>
 <script type="text/javascript" src="index_htm_files/roe.js"></script>
 <!--[if IE]><script type="text/javascript" src="index_htm_files/prs.js"></script><![endif]--><!--[if !IE]>--><script type="text/javascript" src="index_htm_files/prs3.js"></script><!--<![endif]-->
 <script type="text/javascript">var xr_nextpage=""; var xr_transition=0; var xr_transitiontime=0;var xr_prevpage="index_2.htm"; var xr_btransition=0; var xr_btransitiontime=500;</script>
 <style type="text/css">.xr_pbd {position: absolute; border:none; left: 50%; margin-left: -380px;}</style>
</head>

编辑 2:index_3.php 中的某处

<html>
<body>
<div style="width:  480px;  height:  175px;  overflow:  auto;">
<?php include 'get_opwire.php'; ?>
</div>
</body>
</html> 

【问题讨论】:

  • 当您在调用 session_start() 之前已经将内容发送到客户端时,此错误很常见。
  • 是的,但是通过我找不到任何逻辑原因引发此错误。当我删除 sec_session_start();错误消失了,但显然它也破坏了表格。

标签: php mysql session header connect


【解决方案1】:

尝试从所有包含的 php 文件中删除结束 ?&gt; 标记。

【讨论】:

  • 不,不会的。打开&lt;?php 是强制性的(出于显而易见的原因),但可以省略关闭最后一个?&gt;假设它是文件中的最后一件事。事实上,省略它是首选,以便解析器将其后的空格作为“糖”消除,而不是作为“html”转发给客户端。
【解决方案2】:

在会话开始和会话重新生成 id 之前某处的某处将某些内容放在页面上。如果您确定没有在页面上回显或打印任何内容,则可能是其中一个文件顶部的空白区域。

编辑 1

由于编码,某些文本编辑器/文字处理器会在顶部保存带有额外不可见字符的文件。尝试在纯文本编辑器或推荐用于编码的文本编辑器(例如 Notepad++)中将文件的文本复制并粘贴到新文件中。

编辑 2

所以,那些是最有可能的解决方案......它说

output started at /home2/mysite/public_html/mysubsite/index_3.php:7

这意味着它是 index_3.php 的第 7 行输出开始的地方。那里可能有一个空格,或者可能是某种导致发送输出的错误。

如果你真的找不到那个不可见的输出,你可以使用ob_start 等缓冲来捕获所有输出。将 ob_start 放在会话开始之前,稍后使用 ob_end_flush 显示页面。但这并不能真正解决您在不知情的情况下发送输出的事实。

编辑 3:“无输出”包括任何和所有 HTML。

如果任何文件中的某些内容不在 PHP 括号 内,那么这很重要。您的 index_3.php 文件以 HTML 开头。那就是输出。你必须先做get_opwire.php。

编辑 4

关于 get_opwire.php 如何同时具有页面启动内容和表格打印输出,这是一个示例,说明为什么将显示代码与功能代码分开是个好主意。您有三个选择:

  1. 有单独的文件。您将拥有一个类似于 page_start.php 的文件,其中包含您在 index_3.php 的最顶部包含的 session_start,以及一个类似于 display_table.php 的文件,用于显示您在该表所在的位置包含您的表格。
  2. 把表格变成一个函数。您可以将表格输出包装在一个函数中,在 index_3.php 的最顶部包含 get_opwire.php,然后在您想要表格的位置调用该函数。
  3. 使用输出缓冲。输出缓冲捕获打印出来的内容,以便您以后使用。它会是这样的:

index_3.php 的顶部:

 ob_start();
 include get_opwire.php;
 $table = ob_get_contents();
 ob_end_clean();

桌子的去向:

 echo $table; 

【讨论】:

  • 该表在 get_opwire 中回显,但直到会话开始后很长时间。我检查了所有文件是否有空格,但仍然出现错误。
  • 我刚刚添加了 index_3.php 的前几行。第 7 行似乎是 index_3 ...这会导致此错误吗?
  • get_opwire.php 在 index_3.php 的什么位置?您将它包含在最顶部,在任何 HTML 之前,对吧?
  • 啊这当然是我的错误。非常感谢您的帮助! (已接受答案。)如果您有时间,我确实还有一个问题,Index_3.php 是从 Xara Web 开发人员导出的,它包含我在上面编辑的 get_opwire.php。如何在不抛出该错误的情况下仍将 php 拉到表中?
猜你喜欢
  • 2014-06-04
  • 2016-05-28
  • 2018-01-29
  • 2012-05-23
  • 2013-09-11
  • 2020-07-02
  • 1970-01-01
  • 2013-06-08
  • 2016-12-14
相关资源
最近更新 更多