无法修改 PHP 中的标头信息警告消息?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
前面可能有一个空格
标签
You might have a space at the beginning before the
Tag
好吧,我还有其他一些不错的解决方案。
主要问题是你还没有启动OB(输出缓冲)。一旦启动 OB,就不会再出现警告消息。 另一个需要注意的重要点是第一行和第二行。页面的第一列必须是 5 个字符的 PHP 开始标记 (
)。之后没有任何空格字符,按 Enter 键,并为函数“
ob_start()
”提供分号。之后,无论您想要什么,您都可以编写,而不必担心收到那条令人啼笑皆非的警告消息。另外,在整个代码的末尾,您可以调用这个 PHP 函数“
ob_end_flush()
",刷新整个输出缓冲区。但即使不使用此函数,代码也能正常工作,因为默认情况下,PHP 在网页处理结束时输出输出缓冲区的所有内容。希望有帮助。
Well I have some other nice solution.
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.
确保
之前没有空格(包括 BOM 或换行符)
Make sure there's no whitespace (including BOM or newlines) before
<?php
引用维基百科 BOM 条目:
Quoting from the Wikipedia entry for BOM:
还要确保在此之前运行的任何代码(在单独的文件中)可能同时具有
开头和
?>
结尾。如果脚本在
?>
结束标记后使用空格运行(例如,由于愚蠢的编辑器在那里放置了行结束符),PHP 将开始在那里发送输出。简单地省略任何?>在文件的末尾。如果 PHP 到达文件末尾,并且没有 ?>,PHP 将在那里结束 PHPmode。这是已记录的 PHP 功能。
因此,您可以按如下方式重写
database.php
,而无需关闭?>
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 followsWithout the closing ?>
您是否尝试过删除文件顶部的所有空格?
就像这样:
祝你好运;)
Have you tried removing all the whitespace at the top of the file?
Like so:
Good luck ;)
在谷歌上搜索一下:
http ://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/
little search in google:
http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/