开发者

Cannot modify header information Warning message in PHP? [duplicate]

This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers) Reference - What does this error mean in PHP? 开发者_如何学运维 (38 answers) Closed 9 years ago.

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


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


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.


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


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 ;)


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.


You might have a space at the beginning before the

       <?php

Tag


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜