开发者

warning: can not modify header information -header already sent [duplicate]

开发者 https://www.devze.com 2022-12-18 10:52 出处:网络
This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers)
This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers) Closed 8 years ago.

Today first time I use cookie but I am facing a warning

Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\session.php:11) in E:\xampp\htdocs\home_page.php on line 7

This is my code:

<?php
include("session.php");
?>

<?php
$Month = 2592000 + time();    //this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>

<?php
if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
echo "Welcome back!  You last visited on ". $last;
}
else
{
echo "Welcome to our site!";
}
?>

<html>
<head>
    <title>Home Page</title>
    <script src="flowplayer-3.1.4.min.js"></script>

</head>


<body bgcolor="white">
<?php include('search_file.php'); ?>
<font color="red"><b><h2 align="center">Deepak Narwal Welcomes You</h2></b></font>
<hr size="2" width="50%">
<a  href="logout_file.php"><h3 align="right">Sign Out</h3></a>

<a 
    style="display:block;width:250px;height:200px;" 
    id="player">
</a>
<script language="JavaScript">
    flowplayer("player", "flowplayer-3.1.5.swf" , { clip: { autoPlay : false, autoBuffering: true},   playlist: [ 'video.flv', 'flowplayer.flv'] });  //create a object of clip name
</script>


<object classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" align="right" width="320" height="260" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">
<param name="custommode" value="none" />
    <param name="loop" value="true" />
    <param name="autoPlay" value="false" />
    <param name="autoBuffering" value="true" />
 开发者_JAVA百科   <param name="src" value="http://localhost/idiot.mkv" />
<embed type="video/divx" src="http://localhost/idiot.mkv" custommode="none" width="250" height="200" loop="true"  autoPlay="false"  pluginspage="http://go.divx.com/plugin/download/">
</embed>
</object>


<br /><a href="upload_file.php"><h4>Up-Load Files</h4></a>
<br /><br />
<a href="list_files.php"><h4>List All Up-Loaded Files</h4></a>



</body>
</html>

What is this error and how can I hide warnings from my webpage?

Is there any particular place for putting cookies or can I put cookie anywhere like above cookie?

This is my session.php

    <?php
    session_start();
    if(!isset($_SESSION['employee']))
    {
        echo "Your are Logged Out";
        exit;
    }   

    else
    {
    echo "Welcome Mr.".$_SESSION['employee']['username'].";
    }
    ?>


Because you have the setcookie command after a bunch of whitespace. AKA:

?>
             <--- Whitespace
<?php

You can't send the cookie.

You should make this:

<?php
include("session.php");
?>

<?php
$Month = 2592000 + time();    //this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>

this:

<?php
include("session.php");

$Month = 2592000 + time();    //this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>

If you still get this error, most likely you have similar whitespace inside session.php. Also, if you echo anything before the setcookie, you will have the same affect.

'Whitespace' is basically ANYTHING outside of the <?php and ?> tags, and you can't have ANY of this before a header or setcookie command.


In order to get rid of warnings/errors, you would set error_reporting(0).

However, you should only do this in the production stage (when you put the website online). You should program your code so that there are not errors, even with error_reporting(E_ALL).

The warnings are there for a reason. Don't ignore them.


  1. Any whitespace before sending headers is seen as output... therefore, the whitespace between the PHP tags must be eliminated.

  2. error_reporting(0); will supress all errors, but you should seek to fix and understand the errors instead of hide them.

edit: if you still want to report the errors, but just not display them to the user, use this: ini_set('display_errors','off');

edit #2: to clarify, setcookie sends a header to the browser, all headers must be sent before anything is output. Therefore, you should also eliminate OR delay any output in session.php till after the cookies are set.


To send a cookie, the server needs to set one or more Cookie headers. HTTP headers can't be set once the actual body content has started going to the browser. To answer the second question first; you need to ensure that you've set cookies (and any other headers) before you send any output to the browser.

You're seeing this problem because there's whitespace (newlines) being emitted between sets of ?> and <? tags. Kill the whitespace and it should end the problem.

To help with the wider issue, look into using output buffering (see ob_start() et al.) to help prevent output going to the browser before you're ready for it.


You should setcookie as first thing in your code, even before the sesion.php. did you try that?

0

精彩评论

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

关注公众号