无法修改 PHP 中的标头信息警告消息?

发布于 2024-09-12 02:57:42 字数 1668 浏览 1 评论 0原文

我的 index.php 页面代码是-

<?php

if(!$_COOKIE['authorized'] == 1) {
header("Location: login.php");
}

?>


<!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>
<title>My Photo Website</title>
<script src="js/jquery-1.2.6.pack.js" type="text/javascript"></script>
<script src="js/jquery.lightbox-0.5.pack.js" type="text/javascript"></script>
<script src="js/myscript.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/default.css" />
<link rel="stylesheet" href="css/jquery.lightbox-0.5.css" />


</head>
<body>
<form method="post" action="changePhotoTitle.php">
<div id="container">
<h1>My Photos <small>click on the text to change the title.</small></h1>
<a href="login.php?logout=1" id="logout">logout</a>

<div id="main">

<?php require 'getPhotos.php'; ?>

<div id="response" class="hidden" />
</div><!-- end main-->

</div><!-- end container-->
</form>
</body>
</html>

我的database.php页面代码是-

<?php

$db_name = "db";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";

$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());

?>

但接下来的警告消息即将到来- 警告:无法修改标头信息 - 已由第 4 行 C:\xampp\htdocs\pics\index.php 中的(从 C:\xampp\htdocs\pics\index.php:1 开始输出)发送的标头< /强>

my index.php page code is-

<?php

if(!$_COOKIE['authorized'] == 1) {
header("Location: login.php");
}

?>


<!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>
<title>My Photo Website</title>
<script src="js/jquery-1.2.6.pack.js" type="text/javascript"></script>
<script src="js/jquery.lightbox-0.5.pack.js" type="text/javascript"></script>
<script src="js/myscript.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/default.css" />
<link rel="stylesheet" href="css/jquery.lightbox-0.5.css" />


</head>
<body>
<form method="post" action="changePhotoTitle.php">
<div id="container">
<h1>My Photos <small>click on the text to change the title.</small></h1>
<a href="login.php?logout=1" id="logout">logout</a>

<div id="main">

<?php require 'getPhotos.php'; ?>

<div id="response" class="hidden" />
</div><!-- end main-->

</div><!-- end container-->
</form>
</body>
</html>

my database.php page code is-

<?php

$db_name = "db";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";

$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());

?>

but followong warning message is coming-
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\pics\index.php:1) in C:\xampp\htdocs\pics\index.php on line 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等待圉鍢 2024-09-19 02:57:43

前面可能有一个空格

       <?php

标签

You might have a space at the beginning before the

       <?php

Tag

飘过的浮云 2024-09-19 02:57:43

好吧,我还有其他一些不错的解决方案。

<?php
ob_start();

if(!$_COOKIE['authorized'] == 1) {
    header("Location: login.php");
}

?>
<!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">
<!-- Rest of the code follows here -->

主要问题是你还没有启动OB(输出缓冲)。一旦启动 OB,就不会再出现警告消息。 另一个需要注意的重要点是第一行和第二行。页面的第一列必须是 5 个字符的 PHP 开始标记 ()。之后没有任何空格字符,按 Enter 键,并为函数“ob_start()”提供分号。之后,无论您想要什么,您都可以编写,而不必担心收到那条令人啼笑皆非的警告消息。

另外,在整个代码的末尾,您可以调用这个 PHP 函数“ob_end_flush() ",刷新整个输出缓冲区。但即使不使用此函数,代码也能正常工作,因为默认情况下,PHP 在网页处理结束时输出输出缓冲区的所有内容。

希望有帮助。

Well I have some other nice solution.

<?php
ob_start();

if(!$_COOKIE['authorized'] == 1) {
    header("Location: login.php");
}

?>
<!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">
<!-- Rest of the code follows here -->

The main problem is that you have not started the OB (Output Buffering). Once you start the OB, no more Warning messages should crop up. Another important point to note is that the first row & first column of your page must be the PHP start tag (<?php) of 5 characters. After that without any whitespace character, press enter, and provide the function "ob_start()" with semicolon. After that whatever you want, you can write, without the fear of getting that wry warning message.

Also at the end of the whole code, you can call this PHP function "ob_end_flush()", to flush the whole Output Buffer. But without using this function also, the code works, because by default, PHP outputs all the content of the Output Buffer at the end of web page processing.

Hope it helps.

策马西风 2024-09-19 02:57:42

确保 之前没有空格(包括 BOM 或换行符)

Make sure there's no whitespace (including BOM or newlines) before <?php

錯遇了你 2024-09-19 02:57:42

引用维基百科 BOM 条目:

字节顺序标记

许多 Windows 程序(包括
Windows记事本)添加字节0xEF,
任何开头的 0xBB、0xBF
文档保存为 UTF-8。这是
Unicode 字节的 UTF-8 编码
订单标记(BOM),并且通常是
尽管被称为 UTF-8 BOM
它与字节顺序无关。这
如果另一个 BOM 也可能出现
带 BOM 的编码被转换为
UTF-8 而不剥离它。

UTF-8 BOM 的存在可能会导致与
可以处理 UTF-8 的现有软件,例如:

+ 较旧的文本编辑器可能会在文档开头将 BOM 显示为“”,即使 UTF-8 文件仅包含 ASCII 并且可以正确显示。
+ 编程语言解析器通常可以处理字符串常量和注释中的 UTF-8,但无法解析文件开头的 BOM。

Quoting from the Wikipedia entry for BOM:

Byte order mark

Many Windows programs (including
Windows Notepad) add the bytes 0xEF,
0xBB, 0xBF at the start of any
document saved as UTF-8. This is the
UTF-8 encoding of the Unicode byte
order mark (BOM), and is commonly
referred to as a UTF-8 BOM even though
it is not relevant to byte order. The
BOM can also appear if another
encoding with a BOM is translated to
UTF-8 without stripping it.

The presence of the UTF-8 BOM may cause interoperability problems with
existing software that could otherwise handle UTF-8, for example:

+ Older text editors may display the BOM as "" at the start of the document, even if the UTF-8 file contains only ASCII and would otherwise display correctly.
+ Programming language parsers can often handle UTF-8 in string constants and comments, but cannot parse the BOM at the start of the file.

╰◇生如夏花灿烂 2024-09-19 02:57:42

还要确保在此之前运行的任何代码(在单独的文件中)可能同时具有 开头和 ?> 结尾。

如果脚本在 ?> 结束标记后使用空格运行(例如,由于愚蠢的编辑器在那里放置了行结束符),PHP 将开始在那里发送输出。

简单地省略任何?>在文件的末尾。如果 PHP 到达文件末尾,并且没有 ?>,PHP 将在那里结束 PHPmode。这是已记录的 PHP 功能。

因此,您可以按如下方式重写database.php

<?php

$db_name = "db";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";

$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name);

而无需关闭?>

Also make sure that any code that runs before this, in a seperate file may have both <?php-opening and ?>-closing.

If a script is ran with whitespace after te ?>-closing tag (e.g. because of silly editors who put line-endings there) PHP will start sending output there.

Simply omit any ?> at the end of a file. If PHP gets to the end of a file, and it does not have a ?>, PHP will end PHPmode there. This is a documented PHP feature.

So, you could rewrite database.php as follows

<?php

$db_name = "db";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";

$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name);

Without the closing ?>

梦途 2024-09-19 02:57:42

您是否尝试过删除文件顶部的所有空格?

就像这样:

<?php
if(!$_COOKIE['authorized'] == 1) {
header("Location: login.php");
} ?>

祝你好运;)

Have you tried removing all the whitespace at the top of the file?

Like so:

<?php
if(!$_COOKIE['authorized'] == 1) {
header("Location: login.php");
} ?>

Good luck ;)

〗斷ホ乔殘χμё〖 2024-09-19 02:57:42

在谷歌上搜索一下:
http ://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

1) 找到header()语句
导致问题。错误一定是
在此行或之前。

2) 寻找任何可能的陈述
在此之前将输出发送给用户
标题声明。如果您发现一个或
更多,找到一些移动标题的方法
在他们面前发表声明。复杂的
条件语句可能会变得复杂
问题,但它们也可能有帮助
解决问题。考虑一个
条件表达式位于顶部
PHP 脚本决定
尽早标头值并且
将其设置在那里。

3) 确保没有空格
在 php 开始和结束标记之外。
之前有一个空行
开始标签可能看起来很无辜,当
经过PHP处理,它会变成一个
echo 语句打印出空白
线。这是一个常见的罪魁祸首。


little search in google:
http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

1) Find the header() statement that is
causing the problem. The error must be
at or before this line.

2) Look for any statements that could
send output to the user before this
header statement. If you find one or
more, find some way to move the header
statement before them. Complex
conditional statements may complicate
the issue, but they may also help
solve the problem. Consider a
conditional expression at the top of
the PHP script that determines the
header value as early as possible and
sets it there.

3) Make sure there is no white space
outside of the php start and end tags.
While a blank line before the <?php
start tag may look innocent, when
processed by PHP, it will turn into an
echo statement printing out a blank
line. This is a common culprit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文