2

I have a really weird error with a page on my site, and in particular at only one url.

I have a PHP include on the very first line of the page. The very first line of the included file is session_start();

I get this error when the page loads:

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

This is odd enough as the session start is the very first thing on the page and there are no other attempts to start a session. However there's something even weirder going on...

I have many other pages that use basically identical code with different content and they work fine with no errors. I replaced the entire code in the problem page with the code from another page, basically creating a clone. When loading the page the error still happens! WTH?

Any ideas?

Absinthe
  • 3,258
  • 6
  • 31
  • 70
  • show your code. – urfusion Jul 15 '17 at 08:41
  • do you post back the page?, do you include nesting php file? – C.Fasolin Jul 15 '17 at 08:43
  • @urfusion I'm pretty sure the code is only going to obvuscate the question. I'll gladly post if someone can hint at what the issue might be but I don't want this post to get bogged down with pointless code, you know what happens then. – Absinthe Jul 15 '17 at 08:43
  • @C.Fasolin - There is a form on the page which uses post and there are no nested includes. – Absinthe Jul 15 '17 at 08:44
  • normally `headers already sent by` error show when you are using header but there is something printing before that. – urfusion Jul 15 '17 at 08:44
  • @urfusion Thanks, as mentioned it's the very first line – Absinthe Jul 15 '17 at 08:45
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – ponury-kostek Aug 31 '17 at 14:19

2 Answers2

3

You probably have problem with BOM, some editors don't display this mark, so you can't see it, but it's send to browser before session starts.

How to remove BOM on Windows Notepad++

Remove a BOM character in a file

Linux using awk

Using awk to remove the Byte-order mark

ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • Er...OK. What's the solution? Also, as mentioned I copied the entire code from a working page and got the same error. – Absinthe Jul 15 '17 at 08:51
  • You'r using Linux or Windows or Mac? You need some editor that will show you this mark so you can remove it. – ponury-kostek Jul 15 '17 at 08:53
  • Thanks. I'm on Windows, I think the host is using Linux. I'm using Notepad++ (see comment in other answer) – Absinthe Jul 15 '17 at 08:58
2

It may be problem with BOM, use Notepad++ to solve this

  • Download Notepad++ and open the file there, delete all fancy characters before the

  • Make sure there is no whitespace character like " " or tab or linebreak before the

  • In Notepad++ click Encoding and then UTF-8 without BOM to convert the file to UTF-8 without BOM, then save it.

  • Also add ob_start(); before session_start(); to be safe.

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70