Cannot send session cookie - headers alread

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Cannot send session cookie - headers alread

Post by thamizh »

warning : session_start() [function.session-start]: Cannot send session cookie - headers already sent by

After googling i found that i should remove any white spaces before the session_start()
i did that too
but am getting above mentioned warning
y i dont know :(
anyone pls help me
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cannot send session cookie - headers alread

Post by requinix »

What's the code? Start at the very beginning of the file and go up to whatever line the error message says.
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Re: Cannot send session cookie - headers alread

Post by thamizh »

<?php
session_start();
$dbHost = 'localhost';

this is first two lines of the code
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cannot send session cookie - headers alread

Post by requinix »

And there is absolutely nothing before the "<?php"?

What are you using to write code?
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Re: Cannot send session cookie - headers alread

Post by thamizh »

got the result :)
There is a whitespace before the <?php
so the warning comes
sortted out :)
thanks for your help tasairis :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Cannot send session cookie - headers alread

Post by Apollo »

tasairis wrote:And there is absolutely nothing before the "<?php"?
That includes an invisible UTF-8 byte order mark. Can you open the file in a hex-editor?

And is this the file you're calling directly, or is it included from somewhere else?

(edit) oh, solved, nevermind :)
cerberusstudios
Forum Newbie
Posts: 7
Joined: Tue Apr 20, 2010 2:39 am

Re: Cannot send session cookie - headers alread

Post by cerberusstudios »

In PHP 5 you can no longer start your HTML before your session. so even if session_start(); is your first line of code for PHP if you have html before it you will get that error. Even the doctype cannot come before the session (someone please correct me if i am wrong on that, but I am fairly certain). The code you are looking for is similar to the following:

Code: Select all

<?php
session_start();
?>
<html>
</html>
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Cannot send session cookie - headers alread

Post by Apollo »

cerberusstudios wrote:In PHP 5 you can no longer start your HTML before your session.
This has nothing to do with PHP version 5 (or PHP whatsoever).

Sessions = involves cookie, and cookies are HTTP header.
HTML = content that's being sent after the header, by definition.

So once you started generating any actual output, you can no longer send or modify any headers.
Even the doctype cannot come before the session (someone please correct me if i am wrong on that, but I am fairly certain).
doctype = html = part of regular output / content (i.e. can never precede headers)
Post Reply