1

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" />
    <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'].";
    }
    ?>
MackM
  • 2,906
  • 5
  • 31
  • 45
Deepak Narwal
  • 313
  • 7
  • 23

4 Answers4

5

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.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • problem solved..i was using echo in session before setcookie..tell me one thing is it good to set cookie before i check session..beacuse by doing this this error will not be displayed..i sthere any problem in doing this – Deepak Narwal Jan 31 '10 at 23:53
  • Nope, there is no problem by putting the cookie before the session code. – Tyler Carter Jan 31 '10 at 23:55
  • and one more question..i have read out TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it! in my question i am first setting cooky and then retriving its value...is this wrong..should i use isset for checking whether same name cookie exists or not..am i wrong as i wrote code above for cookie – Deepak Narwal Feb 01 '10 at 00:27
  • You shouldn't set a cookie and then retrieve it. You should only set it once for use after the page, and then use internal variables in the script to pass it around. – Tyler Carter Feb 01 '10 at 01:07
4
  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.

Mike Sherov
  • 13,277
  • 8
  • 41
  • 62
  • 2
    +1 for the "fix errors instead of hid[ing] them" comment. Far, far too many people seem to suppress even the simplest warnings, instead of taking the time to check and fix them. Best case scenario, it's fine, and nothing's really broken. Worst case, you're hiding some behaviour that's going to lead to an odd bug further down the line. – Rob Jan 31 '10 at 23:22
  • i dont get ur this line Therefore, you should also eliminate delay any output in session.php till after the cookies are set. I also tried by eliminating white space between two phps but still same error – Deepak Narwal Jan 31 '10 at 23:43
  • I meant eliminate OR delay. Sorry. – Mike Sherov Feb 01 '10 at 03:35
0

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.

Rob
  • 47,999
  • 5
  • 74
  • 91
-1

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

Skuta
  • 5,830
  • 27
  • 60
  • 68