Jump to content

Header/output buffering question


Don E

Recommended Posts

Hello everyone,Today I uploaded some files to an actual server for "live testing" and I got the headers already sent error. I know why this happens but I wanted some suggestions on what to do when you want to redirect a user to another page IF all is well to send them to another page? For example:

$img->uploadImg();  if(!$error = $img->checkError()){header('location: page.php');}else{echo $error;}

I wasn't getting any errors on my home server because output buffering is set to 'On' in php.ini file and 'Off' the 'live server'. What would some of you suggest? Turn on output buffering in php.ini file on live server or instead have ob_start() and ob_flush() for each page when necessary?Also, if it's suggested to do just do it in php.ini, would it be wise to turn it 'On' by having a value instead like so: output_buffering=4096Thanks.

Link to comment
Share on other sites

It seems like it's just as simple as not outputting anything to the page/browser before calling header. Do all your processing/logic at the top of the page, and then determine any user flow afterword. In the logic/processing section, there probably won't be any output. An example of the code causing the issue with the error messages pointing to the line number would be helpful for trying to debug your problem. *by processing, I mean running queries, validating a form, logging a person in, etc.

Edited by thescientist
Link to comment
Share on other sites

I did some changes and I am still getting the error. The following code is at the very top, before any output of html:

<?phpsession_start();include('imgUpload.php');include('page_template.php');	   $imgName = trim(str_replace(' ', '',$_FILES['img']['name']));	   $imgType = $_FILES['img']['type'];	   $imgError = $_FILES['img']['error'];	   $imgTemp = $_FILES['img']['tmp_name'];	   $imgSize = $_FILES['img']['size']; 		$img = new uploadPic($imgName, $imgType, $imgError, $imgTemp, $imgSize);		$img->uploadImg();  	   if($error = $img->checkError())	   {			  $displayError = new page_template();			  $displayError->__set('title', 'Error | site.net');			  $displayError->__set('content', '<p>' . $error . '</p>');			  $displayError->Display();			 exit();	  }?><!doctype html>etc....

This is the error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\site\page.php:1) in C:\wamp\www\site\page.php on line 2 BTW: the includes files don't have session_start();

Edited by Don E
Link to comment
Share on other sites

Are you sure you don't have a line break, a tab or a space? (Or a UTF-8 byte order mark)Be sure to save your file as UTF-8 without BOM.

Link to comment
Share on other sites

Yes file is saved as UTF-8 without BOM. From what I can tell/see, there are no spaces, tabs, line break anywhere before session_start(); I even went to some of the other pages and commented out session_start() in case for some reason it was conflicting with the page above and still get the same error. In other words, from all the pages in the directory for this site, the page above is the only page that has session_start() at the moment.

Link to comment
Share on other sites

Guest LH91325

It's not entirely clear what you're doing, at least not clear from your posts.If your header('Location: page.php') fails with headers already sent then you already sent some output somewhere. It's not clear to me if you understood what Ingolme said. I think he was concerned about the possibility of characters before <?php. I did that once, accidentally inserted a tab before the <?php due to accidental keystroke.BTW what you probably want is die(header('Location: page.php')); But that won't work if you've sent any output.It's not good to go messing with the INI files. That's like killing a fly with a nuclear bomb. Eventually you will end up with a situation where you either can't mess with the INI or you'll end up with a conflict where you want two different settings at the same time.Output buffering isn't good either unless there's no other way. It delays output to your site visitor so any buffering you have will increase page load times, and the goal is to have short page load times.Using sessions isn't good either, not unless you actually need to use them.Just wondering, you started out discussing output buffering and then switched to start_session(). You didn't confuse that with ob_start() did you?Just what page is your listing in post #3. Is that page.php?It would help if you can explain more about what you're trying to do, and what has happened before you got to the point that you decided you want to redirect to another page.I'm sure there is some simple straight forward way to accomplish what you want.

Link to comment
Share on other sites

I am completely aware of what Fox mean't. Before I post questions about anything, I usually do a search on the problem I'm experiencing. I read about all the causes that the problem I'm experiencing can be from tabs, spaces, etc etc, UTF-8 with BOM. I'm aware of this. Yes the page above is called page.php. The above code is at the very top of the page, just the way you see it above. No, not confused session_start() with ob_start(). I was going to make a new thread about this issue but I decided to just post to this thread. Completely disregard the ob_start() but as stated in the first post, the reason why I wasn't experiencing this problem before was because on my home sever(wamp), output buffering was set to 'on' in php.ini. It was until I loaded to a testing live server that I noticed the problem because on the sever, output buffer is off in the php.ini. I went and turned off output_buffering on my home server. Not sure why wamp has it enabled when installing wamp for the first time. You said using sessions isn't good either, can you suggest another way then to pass information from page to page about a user that isn't required to last for a long time? BTW, disregard the first post with the header redirect. In post 3 I mentioned I made some changes. The code in post 1 was on another page, and if all is correct(no errors), then go to page.php(hence the redirect). So I thought why have the form submit to itself like it did in post1, but instead have it submit to page.php to get rid of the header statement thus fixing the header error I was experiencing at first when I first made this thread. But low and behold after doing all that, I am still experiencing the header sent issue. There are no tabs, spaces, etc anywhere before <?php opening tag. I'll even go ahead and make a new file, make sure it's UTF-8 without BOM and see if that fixes the problem, but at the moment I am certain the current file is UTF-8 without BOM. I'm sure it's something simple that's causing this but can be frustrating. :happy0046: Thanks for your input.(everyone's as well!)

Link to comment
Share on other sites

Guest LH91325

You can pass parameters from page to page using query strings. Example: http://example.com/?variable=something&variable2=something+else They will appear in $_GET[] or $_POST[] depending on type of access. Actually I always have forms submit to themselves. It's easy to tell if it's the first access or an attempt at validation. If it's first access they get an empty form. Second and subsequent accesses you can validate all user entered information, then re-enter the valid parts to save the site visitor the labor to enter them again, and you can tell the visitor which fields are invalid, and why. I like to use CSS to put a red border around invalid text entry fields, and then use PHP to add an error message to right telling them what's wrong. Then when they have a successful submission I grey out the input fields and turn them readonly, and add a message something like "thank you your form has been accepted." In contact forms I usually suggest to print a copy of the page if they want to keep a record. I'm very sure it's frustrating. I like programming problems because there's no emotional content other than frustration, unlike real life where there are usually many confounding emotional issues. Talk some more about the problem you're trying to solve and I'm sure we can find a simple solution. Please discuss what user input you are using to decide whether to redirect them to another page. A goal could be to gather and validate all that information before emitting any HTML. Then your die(header(location)) will work. You can pass any info as query strings. Get works very well for this, except only that the user will see the query string in their URL window. If you can't condone that then you'll have to use put, cookies or sessions. Although there's aways another way to do anything and I surely don't know them all.

Link to comment
Share on other sites

Figured out what the problem was... I originally started out in notepad++ with everything and transferred over to dreamweaver(only use it in code view) once my copy of notepad++ was acting up(http://w3schools.invisionzone.com/index.php?showtopic=43320&view=findpost&p=239536) . I was certain I saved the file(s) as utf-8 without bom in notepad++. So now in dreamweaver when doing what I mentioned above about opening a new file, then copying and pasting and saving, low and behold, it works. DW's default(well my version) setting for a page is utf-8 without BOM but gives you the option when saving if you want BOM. If you want BOM all the time, you can go into the settings and set it to automatically to include BOM when saving. (just stating, probably not a good idea ;) ) Once again, thanks for everyone's input! Much appreciated!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...