Jump to content

headers already sent by warning


Petrucho

Recommended Posts

Hi,

 

I had to use Session cookies for a little web application I'm working on, but I can't find solution to the problem mentioned above.

 

I get the warning:

 

 

 

Warning: session_start(): Cannot send session cookie - headers already sent by (...)

 

This warning points to line 1.

 

I've tried to create a new .php file, with the following code:

<?php
session_start();
?>

Only 3 lines of code, using nothing but Windows Notepad and I still get the same warning.

 

I've tried to find solutions but couldn't find any in here, nor in Google.

 

The only explanation I could think of, is a server problem who send headers in each call. It doesn't make any sense though.

 

Someone ever experienced this?

Any thoughts of how to fix this?

 

 

Thanks in advance,

Petrucho.

Link to comment
Share on other sites

I guess this is the source of the problem. the escaped <br />

 

As I mentioned earlier, I opened a new .php file (using Windows Notepad [saved in UTF8]) and written nothing but the session_start();

 

It's really confusing :umnik2:

Link to comment
Share on other sites

Why? if the php session start code is at the very top of page before <!doctype> and any other html including body tag which the <br/> can only be placed would the <br/> appear?

 

You are not including this in another php file are you? The session_start() must, if included into another php file appear before that php files html <!doctype> tag if present.

Link to comment
Share on other sites

the only lines of code in the .php file I've created are:

<?php
session_start();
?>

No, this file is not included.

 

I've created this file, just to check if I still get the warning, and yes, I do.

 

It doesn't make any sense and that's why I came here.

What else could send something to the buffer? maybe settings of the server? php.ini configuration?

 

I can't find any solution and yes, I think, something's adding the <br /> prior to the session_start(); which makes it impossible to send any headers.

 

Edit:

 

Maybe this would help;

 

Link to test.php: link

 

Code of test.php as mentioned above (on this comment)

Edited by Petrucho
Link to comment
Share on other sites

It looks like the <br /> is just part of the error message and not the cause of the problem.

 

If your document is saved as UTF-8 with a byte order mark (BOM), the BOM will be sent to the client before any of the PHP starts running, which would cause that issue. I'm not sure if Windows notepad adds the byte order mark or not, notepad is not the ideal environment to write code in.

 

Aside from that, make sure there are no line breaks, tabs or spaces before the opening <?php tag.

Link to comment
Share on other sites

Problem fixed!

Ingolme, dsonesuk - thank you both for the kind assistance! :good:

 

I used netBeans as my editor and only for this example, I've tried Notepad.

 

As I said, I have no code in the test.php file. only the 3 lines of code I wrote which are the opening php tag, closing tag and in the middle, the session_start(); function.

 

After googling about BOM, I've decided to install Notepad++ (version 6.9.2) which gives you the option of saving the file without the BOM.

 

Problem got fixed.

 

For someone who bumped into this thread in the future:

It seems the only problem was indeed, the BOM as Ingolme has explained.

Notepad++ gives you the option to save without BOM. (under Encoding->Encode in UTF-8 without BOM)

In addition, not just that the BOM character sent to the output, but also could cause (at least to Google Chrome) a not welcoming space at the top of the page.

 

First time I'm experiencing this. also the first time I'm using JetBrains for php coding.

 

 

Thanks again!

Edited by Petrucho
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...