• PHP Help PHP Coding
  • [RESOLVED] Warning: session_start(): Cannot send session cookie - headers already sent

I am also getting this error. I've looked around for a while and haven't figured out how to fix it, but my errors are for all of my PHP code. This is kind of weird. So here is my code.

<?php session_start();?><html><?php
if(isset($_SESSION['loggedinuser'])) 
echo "You're logged in! Click <a href='profile.php'>Here</a> to view your profile";
else {
echo "You're not logged in. Do you want to <a href='login.php'>log in</a> or <a href='register.html'>register</a>?";
}
?>
<head>
    <title>UrComics.com Your comics, Your votes!</title>
    <link rel="stylesheet" type="text/css"
    href="style.css" />
</head>
<body>
<table border="1" cellspacing="0" cellpadding="1" align="left">
<tbody>
<tr>
<td>
<img alt="" src="./Images/UrComicsLogo1.gif" />
<br />
<h5 align="center">
<a href="index.php">Home</a>
<br />
<a href="UrPaper.php">Ur Paper</a>
<br />
<a href="inThePaper.php">In The Paper</a>
<br />
<a href="Help.html">Help/FAQ</a>
<br />
<a href="uploadcomic.php">Submit A Comic</a>
</h5>
</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="2" cellpadding="2" align="center" width="75%">
<tbody>
<tr>
<td>
<h1 align="center">Welcome to UrComics.com</h1>
</td>
<td>
<h3>Hits</h3>
<hr align="left" width="100%" "noshade" size="2" />
<h3>
<?php
$filename = "counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose ($fd) ;
$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout = fwrite ($fd , $fcounted) ;
fclose ($fd) ;
?>
</h3>
<br />
</td>
</tr>
<tr>
<td width="50%" class="red">
<h3 align="center">Announcements</h3>
<hr align="left" width="100%" "noshade" class="red"  size="2" />
<br />
<p>Welcome to urcomics.com where you can post your own comics and see what others think of your idea.
Just create an account to get started!</p>
<a href="register.html">Click Here to Get Started!</a>
</td>
<td width="50%" class="blue">
<h3 align="center">Comics Of The Week</h3>
<hr align="left" width="100%" "noshade" class="blue" size="2" />
<br />
<p>Once I figure out the php i am gonna put the top rated comics of the week on here.</p>
</td>
</tr>
<tr>
<td class="green" align="left">
<h3 align="center">Login</h3>
<hr align="left" width="100%" "noshade" class="green" size="2" />
<br />
<?php
if(isset($_SESSION['loggedinuser'])) {
?>
You're currently logged in, do you want to <a href="logout.php">Log out</a>?
<?php 
} else { 
?>
<p>Put in your username and password to login.</p>
<br />
<form name="authenticate" method="post" action="authenticate.php">
Username:<input name="username" type="text" size="20">
<br />
Password:<input name="password" type="password" size="20" />
<br />
<input type="submit" name="submit" value="submit"/>
<input type="reset" name="clear" value="reset"/>
</form>
<?php
}
?>
</td>
</tr>
</tbody>
</table>
</body>
</html>

So I am getting these errors at the top of my page.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\file\secret\index.php:1) in C:\anotherfile\file\index.php on line 1

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\files\morefiles\index.php:1) in C:\morefiles\stuff\index.php on line 1

NOTE:That is not really the file location.

But it is weird because the code was working then it just stopped working. and none of my php works so I don't know if this is a problem in my php.ini file or what. Anyone know how to fix this?

    Some editors, when they save a file as UTF-8, stick extra characters into the file at the very beginning (the format doesn't require it, but they do it anyway). Then refuse to show you those characters when they display the file. To see if your editor is one of these, try saving an empty file and seeing how many bytes it takes up. If it's more then zero (probably three), then you'll have to tell your editor to stop messing around (and how you do that depends on the editor).

      I don't think so... I am using Microsoft Visual web studio and it doesn't do that because when I wrote this code I wrote it in notepad so It shouldn't be that that is happening.

      This is really strange

      Thimbletack

        The error message is specifically telling you that something on line 1 of the script generated output prior to the session_start() call. Either there is something before the opening <?php tag (a space, a newline, a unicode BOM character), or the only other thing I can think of would be if your PHP configuration has an auto_prepend_file specified and that file is generating output, although in that case I think the error message would point to that file instead (though I'm not 100% sure about that).

        Therefore pending any further information, I suspect Weedpacket's suggestion is correct, that your editor is saving the file in UTF-8 with a BOM (byte order mark) at the start of the file. Either see if there is an option with your editor to save it as plain ASCII or as UTF-8 without the BOM, or else try another editor that can.

          Thimbletack wrote:

          I wrote this code I wrote it in notepad

          Yes, Notepad is one of those editors that does this (unless you save the file with an ANSI character set). Visual Studio also provides the option, but if I recall, for HTML files it defaults to Latin-1 and you have to tell it to use UTF-8 (with BOM).

            Ok so I saved a blank file and it put 3 bytes and said 1 kb so I do think you are right. So I don't think notepad does that so I am probably going to edit it in there. But is there some way to change the code to have nothing in front of it? because in notepad I see nothing so do you guys think the best way to do this would to open a new notepad file and copy the code down?

            Thanks for your help,

            Thimbletack

              Sorry for double posting but I just wanted to say thanks for helping me out. I rewrote the code in notepad and it worked! So I just gotta rewrite each page w/ php on it... But anyway thanks again.

              Thimbletack 😃

                a year later
                Weedpacket;10897309 wrote:

                Yes, Notepad is one of those editors that does this (unless you save the file with an ANSI character set). Visual Studio also provides the option, but if I recall, for HTML files it defaults to Latin-1 and you have to tell it to use UTF-8 (with BOM).

                Yes that's right. I had the same problem with few files created by Notepad and i was quite confuse.
                My problem came from the ANSI character set UTF-8 with BOM.
                After a lot of research i found this this information then i fixed the problem.

                I just recreated the file with different editor and everything works fine.
                I hope this information can save your time and nerves. 🙂


                Nothing is imposible imagination is everything!
                Benchmark Software
                [URL="ttp://stsdb.com"]World's fastest database[/URL]

                  11 days later

                  I had this problem. Went through and cleaned up all spaces and lines. It still didn't work. Then I changed from UTF-8 to ASCII. Bingo! So now I'll try to use ASCII for php and UTF-8 for HTML

                    @: Your problem wasn't UTF-8 itself (I use it on several projects), but rather than your text editor probably included the BOM character at the beginning of the file. Since the BOM isn't part of "<?php", PHP assumes that you meant for it to be outputted to the user.

                      If you just use Notepad to write your scripts then i would highly encourage you to download Notepad++. Quite easily the best text editor for web design available. And its free! :p

                        Great! That worked as well. I downloaded notepad++ and saved as UTF-8 without BOM.
                        Thanks

                          2 months later

                          After scratching my head with the same problem, i found that there was a new line after the closing ?> php tag in one of my include files. Like others have mentioned, this is a definite 'gotcha'

                            2 months later

                            I know this is an old thread but perhaps someone can help.

                            I have created a test file with ONLY this code:

                            <?php
                            $value = session_start();
                            echo "Session value = ".$value."<br>";
                            ?>

                            As suggested in this thread (or elsewhere):
                            1) There are no spaces, html, etc. preceding this.
                            2) The file is encoded in UTF-8 WITH BOM
                            3) I've modified my php.ini file to set: session.cache_limiter = public

                            And I get these errors:

                            Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2

                            Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2
                            Session value = 1

                            I am baffled !

                            Does anyone know how to fix this ??

                            Thx !

                              while it shouldn't matter, does it still happen if you don't assign the session_start return value to a variable?

                                ... headers already sent by ...

                                means that there is something in front of the session_start

                                2) The file is encoded in UTF-8 WITH BOM

                                I believe that is suppose to be without BOM. But save yourself the hassle and get anyone of the hundreds of free IDE's out there. NotePad++ is a good one to start with.

                                  Kirk,

                                  You were right about UTF-8 WITH BOM !

                                  I was already using notepad++.
                                  When I converted to to UTF-8 WITHOUT BOM the warning disappeared !

                                  Thanks immensely for your help !!!

                                  Dagon -- thank you too for your quick reply !

                                  🙂

                                    6 months later

                                    I'm using Notepad++ and had the PHP down in the body. Once I moved it to the very first line it was fine. Even the <html> tag caused it to break if it was first. This works:

                                    <?php
                                    session_start(); // initialise a session - commented out as it seems to auto start
                                    $SESSION['counter']++; // increment a session counter
                                    ?>
                                    <html>
                                    <head>
                                    </head>
                                    <body>
                                    <?php echo "You have viewed this page ". $
                                    SESSION['counter'] . " times"; ?>
                                    </body>
                                    </html>

                                      Since [man]session_start/man modifies the outgoing HTTP response headers, it doesn't have to be the 'first' line of code - it just has to be called before any output (which causes the HTTP headers to be sent at that point).

                                        a year later

                                        I enclosed the start_session() command in php tags at the beginning of my script and then started a new php tag for the rest of the code. This is the only solution that worked for me, as follows:

                                        <?php session_start();?>
                                        <?php
                                        php code here......
                                        ?>