PhP Mysql 如何正确使用 include,避免 headers 错误

标签 php mysql html include

兄弟们。我在这里写这篇文章的后续问题:Understanding cache limiter | headers already sent php warning 再说一次,我相对较新,但我跟随得相对较好,但这让我感到困惑。

问题说明:

  • 我正在使用 Xara Pro 9 导出页面
  • 里面是嵌套的php脚本,用于在整个页面上发布各种信息
  • 我收到“ header 已发送”警告,因为 php 位于代码深处
  • 此外,如果我尝试添加其他.php 脚本,它通常最终不会显示或破坏之前的脚本。

因此,将其分解,Xara 导出index_3.html(我将其更改为index_3.php,以便它可以解析。)Xara 文档的开头如下所示:

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

我知道现在包含 php 已经太晚了,因为它是输出。 Xara 在所见即所得编辑器中使用 html 占位符。然后在实际的index_3.html(php)代码中,php包含在文档中,如下所示:

<div style="position: absolute; left: 47px; top: 39px; width: 503px; height: 256px; overflow: hidden;">
<div style="width: 503px;  height:  256px;  overflow:  auto;">
<?php include 'get_opwire.php'; ?>
</div>
 </div>

(至于为什么它在 div 上加倍,我不知道)...无论如何, get_opwire.php 显示一个像它应该的那样的表格,但它会抛出那些标题警告。另外,在index_3的更下方,还有另一个php..

<div style="position: absolute; left: 30px; top: 533px; width: 100px; height: 26px;">
<div style="width: 100px;  height:  26px;">
<?php include 'usernameget.php'; ?>
</div>
 </div>

usernameget.php 拒绝显示,除非我摆脱 opwire,就好像 xara 只想处理 1 个 php 包含一样。

因此,为了修复警告,我在上一个问题中得到了 3 个选项

1.) Have separate files. You would have a file like page_start.php that does includes and session_start that you include at the very
top of index_3.php, and a file like display_table.php that displays
your table that you include where the table goes.

2.) Turn the table into a function. You would wrap the table output inside a function, include get_opwire.php at the very top of
index_3.php, then call the function down where you want the table.

3.) Use output buffering. Output buffering catches the stuff printed out so that you can use it later. It would go like this:

我已经对这三个选项进行了一天的实验,尤其是选项 3,但我对 php 还不熟悉,无法将它们组合在一起。我还想从逻辑上理解为什么第二个 php include 失败,并知道我实际的最佳选择是什么。非常感谢您的帮助!

编辑:错误:

[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

最佳答案

session_start() 和任何其他 session 操作(创建 session ID、移动 session 文件夹)必须在任何输出发送到浏览器之前完成。尝试将其作为索引/主文件的第一行,并确保它不在任何其他文件中。

关于PhP Mysql 如何正确使用 include,避免 headers 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306912/

相关文章:

php - Referer 和 Varnish 缓存条件代码不起作用

php - PHP 的 substr 效率如何?

php - 使用 Union All 添加按日期顺序回显 SQL 数据

javascript - 从 JSON 生成的下拉选项中删除重复项

javascript - 如何在html页面中显示php错误信息?

php - Laravel 5 - 查找模型的分页页面

mysql - 根据列名计算总和

android - 将 android 与 MS SQL SERVER 2008 连接

html - IE 中的文件上传按钮和奇怪的文本光标行为

php - 将敏感数据安全地存储在数据库中