-2

Possible Duplicate:
Headers already sent by PHP

"Cannot modify header information - headers already sent"

Since updating my MacBook Pro to OS X Mountain Lion, my localhost website has been malfunctioning.

The majority (if not all) of my header("Location: ./xxx.php"); redirects have not been working. They worked in Lion, so I'm assuming Mountain Lion is the cause for this and not my code. The error also occurs if I turn off the built in server and enable/use MAMP.

This leads me to believe it's an issue with how Mountain Lion as a whole treats headers. If this error was in fact my code, then why'd it work in Lion? I'd assume Mountain Lion reads PHP code in a stricter way if this is the case. Also, I then made a simple test.php script with a header redirect to see if headers in general weren't working. As it turned out, that script redirected me with no issues whatsoever when I opened it in Safari. This error seems odd to me, and I'd like some feedback. Thanks.

Community
  • 1
  • 1
user1585762
  • 59
  • 1
  • 6
  • 3
    You can't send headers once output is sent. The error says it all – Cole Tobin Aug 08 '12 at 19:45
  • "Remember that header() must be called before any actual output is sent" http://php.net/manual/en/function.header.php – TheZ Aug 08 '12 at 19:46
  • Maybe you added a whitespace as the first character in your PHP-file by accident or something like this, that breaks it. – insertusernamehere Aug 08 '12 at 19:48
  • Grab Firebug, or some other debugging tool. I would think that maybe the new php configuration is causing something to be sent before you get to the your header() code, so if you know what it is then you'll have a better chance to find what's causing it. – Robert Aug 08 '12 at 19:50
  • 1
    _"THEN WHY DID IT WORK IN LION????"_ - Have you reinstalled MAMP? Does MAMP load the same php.ini as before? Have you perhaps edited a file before updating and did actually not test it before? You really can't blame an OS update for breaking a non-OS application on that level. – CodeCaster Aug 08 '12 at 19:51
  • See "[But it worked on the other server!?](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php#~other+server)" – mario Aug 08 '12 at 20:02

1 Answers1

2

Cannot modify header information - headers already sent means you are trying to send a header when data has already been sent. Which is impossible.

If you need to debug your application, replace your header('Location: ...) by a simple exit; and check if you have any output. Search for any echo, print... before your header() calls.

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • It worked on both the built-in sever AND on MAMP prior to updating to OSX ML. I always only have one on at a time. And no, no output before the header redirects in the form of echo/print. There are a few
    tags w/ the doctype and such near the top beneath the session_start()/sessions, but I don't know if that's considered enough to break the script. The html stuff is NOT in the PHP tags.
    – user1585762 Aug 08 '12 at 19:55
  • I can post my login script which is one of my scripts experiencing this issue, if you'd like. – user1585762 Aug 08 '12 at 20:01
  • @user1585762 "A few
    tags" is considered output. Anything that will be visible in your browser or console is considered output.
    – Tchoupi Aug 08 '12 at 20:03
  • Is there a way around this? Maybe a php.ini file change on how it handles headers beneath output? It worked before Lion, so I'm guessing that was either a bug and/or a php.ini setting (which was nice to have) – user1585762 Aug 08 '12 at 20:28
  • @user1585762 If output buffering was enabled http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering this would fix the error. But be aware that it is much cleaner to modify you code and not rely on this setting. – Tchoupi Aug 08 '12 at 20:30
  • So if I rely on the setting, what happens if I ever put the site up live on the internet? Will that setting end up meaning nothing, and thus the user will experience the header issue, or can I put that setting up online with my current localhost site? – user1585762 Aug 08 '12 at 20:32
  • @user1585762 You can use it. But what I meant is that you can end up doing an upgrade, changing OS (what you did), switching host... and it will break your site. The function `header()` has a good reason to be called as such. – Tchoupi Aug 08 '12 at 20:43
  • Is there a way to do something similar to header redirects (instead of javascript)? I heard meta refreshes work, but don't know much about using those on php scripts. – user1585762 Aug 08 '12 at 20:46
  • @user1585762 Why are you using `header()` for? To redirect to a login page? Something else? – Tchoupi Aug 08 '12 at 20:56
  • How do I reenable the setting? In the php.ini I assume (I forgot the directory path for it for both the built-in server and for MAMP). Thanks. ex: redirect after login credentials are confirmed valid to member page. Logout, profile system redirecting, register redir. to home page after form filled out, and logout red. to login page – user1585762 Aug 08 '12 at 20:57
  • 1
    @user1585762 Yes in the `php.ini`. I sent you the link to the setting earlier. If you are using a redirect, it needs to be secure. Using a meta refresh is everything but secure, because it will still load the page you're trying to protect. The best thing to do is to fix your code and move your `header()` where they belong: before the actual output. – Tchoupi Aug 08 '12 at 21:04