-1

Possible Duplicate:
Headers already sent by PHP

I am trying to access DOMPDF (a script that converts HTML to a PDF) within a SESSION protected area of my website.

The SESSIONs contain user info and when you click a button the page refreshes and downloads that page as a pdf. This works fine on my dev server on my laptop. But online it says headers already sent.

I printed the headers which are below and have traced it back to my SESSIONs. Is it the case that as soon as you initialise a SESSION headers are sent?

array(3) { 
  [0]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT"
  [1]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate,post-check=0, pre-check=0" 
  [2]=> string(16) "Pragma: no-cache"
}
Community
  • 1
  • 1
Somk
  • 11,869
  • 32
  • 97
  • 143
  • 1
    It doesn't mean that session headers have been sent. It means that output has started (i.e. the script has printed something on the web page) and the server has had to send headers already, so you can't modify them anymore. **The error message tells you where the output has started, start your debugging there.** – JJJ Jul 25 '12 at 15:49
  • 1
    Are you hosting you project at a freehoster? They send some ads sometimes and you will get such errors. – nkr Jul 25 '12 at 15:49

4 Answers4

1

You have to start the session before you send anything to the user. You can use output buffer (ob_* functions, like this and this) to prevent sending anything when script shouldn't do that.

mdziekon
  • 3,531
  • 3
  • 22
  • 32
0

There are a number of things that can cause this, but what's most likely occurring is that you have some whitespace being printed to the screen without your knowledge prior to the headers being sent.

Remove any unnecessary close-php tags (?>) from the end of your scripts. This should help (and is a best-practice).

Matt
  • 6,993
  • 4
  • 29
  • 50
0

Starting a session does not send headers, but it would try to set a cookie which needs to be sent with the headers. You are probably calling the session_start() after having already output something to the browser.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

The answer may lie in the PHP documentation for session_start():

If you are receiving errors like:

ERROR Warning: session_start() [function.session-start]: "Cannot send session cookie - headers already sent by (output started at... "

...or...

"Warning: session_start(); [function.session-start]: Cannot send session cache limiter - headers already sent".

...ensure that session_start(); is literally at the very top of your script, even before any DOCTYPE declaration, eg:

...

msanford
  • 11,803
  • 11
  • 66
  • 93