Jump to content

Stumped by new header error - argh!


mmosel

Recommended Posts

Well, I'm back with another error and this time I really stumped.

I'm getting the good ol' nasty cannot modify header info - headers already sent error. The thing that gets me is that I cannot pinpoint the problem. It seems to be very unpredictable.

For the longest time I've been programming this project without a problem. I have four sections of info, the header or banner/ nav area, the right column, the main page, and the footer. Most of my application is happening on the main page. My index.php includes four files - header, rcolumn, main, footer -in that order.

I have php outputting the navigation and user info in the column and everything works fine. I have my main scripts doing all sorts of things. Often, I'll use a header() command to redirect or reload a page (like in the cart). But for some strange reason, when I run a script for publishing a product, I get this header error. This script has been running just fine for weeks. The problem comes when I add more output in the right column page. If I have too many echo statements, or if the html is too long, then I get the error. But if I minimize this, the error disappears! I can't understand why it sometimes works and sometimes does not. I would think that if it was because of an echo statement ahead of it, then it would never work at all, no?

I hope someone can help me understand what might be happending here. My head is starting to hurt with this one. Ouch.
Link to comment
Share on other sites

Well, I'm going to use ob_start() for this particular script. However, I would still like to know why this is happening so unpredictably. I don't know if I should use the ob_start for other scripts that use a header() function or not. Strange and frustrating.
Link to comment
Share on other sites

Well, without seeing your code I can't tell you where you went wrong, but I can tell you that if you have any browser output then the headers are through and none can be sent after that, so if you have any echo commands that may execute before your header() remove them or move the header() or use output buffering. Headers must be sent to the browser first. You can't send a header at the end of the document, so the reason it probably gets rather random on you is that at some point you have an if statement that fires under certain conditions and creates browser output. Since this fired the header() can't be used anymore on that script during that execution. I haven't seen the code so I can't say for sure, but that's the number one reason that springs to mind.
Link to comment
Share on other sites

Thanks for your reply. I appreciate it.

(note: when I say I use a header() function, the only function I've been using is the header( "location: ") - if that makes any difference.)

I understand what you're saying, but my experience has been mixed. I have output, such as navigation, which is output via echo by php. The navigation is working fine. Later on down, in another script that runs after the navigation output, I call the header() function in my cart and it works just fine. It reloads the page and all is well. I also use a custom error function that calls a header() function that works after stuff is being echoed to the browser. However, I use multiple scripts, not just one big long one. I don't know if this would have any affect on this issue. But if my programming should NOT be working, but yet IS working, I'd like to know why!

Posting specific code for that problem that I had would be a little nutty, because it's a very long script. The script itself functions just great. No functionality errors. Just that header already sent error that was unpredictable at best. According to theory, my scripts that call a header() shouldn't work at all, but they do! I wish I knew!
Link to comment
Share on other sites

It makes no difference that it is a location redirect, it is still a header. I can offer a solution. It is not the best way to resolve the problem as it requires using JavaScript which all clients may not allow to run on their browser, but here goes...replace the header("Location: {url}"); with

[code]<?php echo "<script language=\"JavaScript\"> top.location.href = '{url}'; </script>\r\n";[/code]

That's it, now you should have a redirect that won't care if browser output was already started. Keep in mind all your clients may not use JavaScript enabled browsers or they may deactivate it. Until you can resolve your header() issue, or get someone to review your script to find the problem, this may be a temporary solution that fits the need.
Link to comment
Share on other sites

Hi. Thanks again for your reply.

I appreciate that code snippet, and I may use it.

However, I definitely prefer to use php if I can.

I am using ob_start() and flush and it's working fine. If outputing any code to the browser before calling the header() function is the problem, then I don't really need anyone to look at the code, because I know exactly where I'm doing that. My question is why would the error sometimes NOT occur when in fact I am echoing content and html before the script that calls the function? Maybe it's a timing thing, or the speed of the script? I don't know, but I may just end up using the output buffering for all my scripts.
Link to comment
Share on other sites

The key is that you have to go with what works for you. I believe the problem is that there is a particular line somewhere above the header() that fires on those occasions that it fails and the conditions of it are not met for the times it succeeds. I hope that makes sense to you.
Link to comment
Share on other sites

[!--quoteo(post=354084:date=Mar 12 2006, 12:15 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 12 2006, 12:15 AM) [snapback]354084[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The key is that you have to go with what works for you. I believe the problem is that there is a particular line somewhere above the header() that fires on those occasions that it fails and the conditions of it are not met for the times it succeeds. I hope that makes sense to you.
[/quote]

Well, yeah, in theory, what you are saying makes sense to me. But the fact remains that the condition that changes in this case isn't the fact that html and other content is echoed to the browser before header is reached in the script - it's that the number of lines of output changes. If you could see it, you'd understand why it's stumped me - because it defies the logic that says this is wrong, and the error should occur with ANY output before the header() is called - but that's just not the case.

I should also note that I'm running this site development on my localhost Apache server on Win2k. When I run this on my live server, my results could change and perhaps the error will be more consistant. Thankfully I have the output buffer!
Link to comment
Share on other sites

Well, yes, your output could vary from server to server depending on server versions and configurations. You might get different output on my server because it is linux kernel 2.6, apache 2, php 4 and mysql 4. Then there are the configuration files which can be set up multiple ways to ultimately achieve the same general goals.
Link to comment
Share on other sites

[!--quoteo(post=354162:date=Mar 12 2006, 07:47 AM:name=saiko)--][div class=\'quotetop\']QUOTE(saiko @ Mar 12 2006, 07:47 AM) [snapback]354162[/snapback][/div][div class=\'quotemain\'][!--quotec--]
put ob_start(); at the start

and ob_end_flush(); at the end.
if u havent already
[/quote]

Thanks. Already have that setup. Appreciate the post.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.