-3

Possible Duplicate:
Headers already sent by PHP

I have a php webpage that displays a drop down menu

The drop down menu provides links to a back-end script that will run a query and set some session variables. Then it redirects to the results page with

header('Location: search_results.php');

the redirect dies with "Cannot modify header information - headers already sent"

trying to figure out a way around this. The "Modify header" error is an error that's always local from the PHP script that's currently running? Or is it the whole session in general?

thanks, JM

Community
  • 1
  • 1
user1349089
  • 127
  • 6
  • 15
  • It sounds like something is already echoed, at which point you can no longer send header information. – ACJ Aug 22 '12 at 21:40
  • It means that some output was already produced by the script calling `header()` function. Figure out what's being printed, and get rid of it. – moonwave99 Aug 22 '12 at 21:41
  • 2
    If I had a dollar for every time someone asked a question about *"Cannot modify header information - headers already sent"* ... geez people, use the search or the google. This has been asked and answered **many** times. –  Aug 22 '12 at 21:42
  • @rdlowrey - ...and, if I had a dollar for every post in every support site (not just here) that said "jeez this question has been answered sooooo many times..." now we're talkin'! :-) My apologies for the subject-repeat. – user1349089 Aug 22 '12 at 21:53
  • 1
    @user1349089 Hey, let's just use the site with no regard for the rules! Woohoo! It's in the FAQ. People get annoyed in any community when you don't follow the rules. –  Aug 22 '12 at 21:55
  • 3
    @user1349089: That's no excuse for not searching, and if you have many dollars from that, maybe the problem's with you, and not with us. The fact is, the question **has** been asked a multitude of times in the past, so many in fact, that it has **its own catch-all question** (Which your question is about to be closed as a duplicate of). If you had bothered merely searching the exact wording of your error, you would have found what you needed. Don't automatically go to "support sites" and don't expect people to yell at you if you fail to follow basic searching. – Madara's Ghost Aug 22 '12 at 22:00

4 Answers4

3

Make sure you're not outputting anything before that line (such as whitespace, or other output).

An easy way to do this is to turn on the output buffer:

ob_start();

And clear it when appropriate:

ob_end_clear();  // erases output buffer
// or 
ob_end_flush();  // sends output buffer to screen

However, the proper solution is to go through your code and ensure there are no stray output statements.

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

Without looking at your code, I'll hazard a guess that you have HTML before the header PHP code. Headers need to be parsed before HTML.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • thanks everyone! on the html output BEFORE the header() statement, here's my script: does the script that called this script count as output? – user1349089 Aug 22 '12 at 21:44
  • Is that `header('Location: search_results.php');` inside the loop? You can't do that, because you can't send the header more than once. – Steve Aug 22 '12 at 21:50
  • true - inside/outside, the loop only occurs once, but thanks. I probably posted the script in one of the the many flavors of trying to get it to work - sorry – user1349089 Aug 22 '12 at 21:56
0

You can try using output buffering by issuing an ob_start() at the beginning of the PHP script.

A common cause of this error is having a whitespace (or sometimes a BOM) before the starting <?php tag; the Header check is local, it is not session-wide. On the other hand, it is influenced by other included PHP files, so you'll have to check them too.

LSerni
  • 55,617
  • 10
  • 65
  • 107
-1

You can use a meta redirect

<meta http-equiv="refresh" content="2;url=http://google.com/">

The above will refresh the page in 2 seconds to http://google.com/ Then javascript redirect

<script type='text/javascript'>
document.location.href='http://google.com';
</script>
rsz
  • 1,141
  • 1
  • 18
  • 38
  • The js works. thanks man – user1349089 Aug 22 '12 at 21:58
  • There are two issues I see with the JS approach: 1) doesn't work with JS disabled 2) you are doing a full round trip to the client instead of handling the redirect on the server side. – Steve Aug 22 '12 at 22:00
  • @Steve If were javascript blocked then you cold not post here on stackoverflow, today there are only few users who has javascript disabled maybe – rsz Aug 22 '12 at 22:03
  • yeah I'll have to go back and re-tool the thing. PHP is not my forte. thanks again. – user1349089 Aug 22 '12 at 22:10
  • @user1430562 - About two percent of all users surf the net without JS enabled. You might not care about 2% of your user base, but that doesn't change the correctness of my statement. The JS approach does not work with JS disabled. More importantly though, my second issue still stands: You are doing a full roundtrip to the client. – Steve Aug 22 '12 at 22:10