sdguy Posted June 24, 2008 Share Posted June 24, 2008 I'm working through the Lynda.com php video tutorial and for some reason the author has no problem putting a: header("Location: www.whatever.com"); exit; in the middle of the document and redirecting to a new page, but whenever I do it I get something like this: Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\widget_corp\create_subject.php:16) in C:\Program Files\xampp\htdocs\widget_corp\includes\functions.php on line 21 I was able to get it to work when I put header() as the very first line in the document, but I want to be able to redirect based on actions throughout the document. Anyone have an idea why this guy on the tutorial can do it no prob but I keep getting errors? Thanks. Quote Link to comment Share on other sites More sharing options...
peranha Posted June 24, 2008 Share Posted June 24, 2008 If you can post the code, we can help. you are outputting data to the page before the the header call is made. that is what this error means. Quote Link to comment Share on other sites More sharing options...
trq Posted June 24, 2008 Share Posted June 24, 2008 Did you read the big HEADER ERRORS sticky at the top of this forum? This is a common mistake. Quote Link to comment Share on other sites More sharing options...
sdguy Posted June 24, 2008 Author Share Posted June 24, 2008 This is the page, it's receiving data from a form and redirecting back to the main content page if successful. The data is being properly moved into the database, but I just don't get the proper redirect. <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); ?> <?php $query = "INSERT INTO subjects ( menu_name, position, visible ) VALUES ( '{$menu_name}', {$position}, {$visible} )"; if(mysql_query($query, $connection)) { //Success! redirect_to("content.php"); } else { //Display error message echo "<p>Subject creation failed</p>"; echo "<p>" . mysql_error() . "</p>"; } ?> <?php mysql_close($connection); ?> The redirect_to function is just this: function redirect_to( $location = NULL ) { if($location != NULL) { header("Location: {$location}"); exit; } } I read on the php.net site that headers can't be sent after other info has been, but it also says that the header("Location: ...") function is an exception, plus I'm watching this guy do it and figure it must be a preference set in php or on my local web server (using xampp). Any help is appreciated, but I'm pretty new to php so please dumb them down if possible. Quote Link to comment Share on other sites More sharing options...
sdguy Posted June 24, 2008 Author Share Posted June 24, 2008 Gah! I didn't even see the sticky, I think my eyes are a bit foggy at this point. I'll check it out and hopefully figure out the problem. Quote Link to comment Share on other sites More sharing options...
peranha Posted June 24, 2008 Share Posted June 24, 2008 Here you are echoing out text causing it to not be able to redirect on a failure //Display error message echo "<p>Subject creation failed</p>"; echo "<p>" . mysql_error() . "</p>"; Quote Link to comment Share on other sites More sharing options...
gijew Posted June 24, 2008 Share Posted June 24, 2008 Every time you echo something out you're telling the browser to say (x). This being said you can also send output to the browser when you have things like mysql errors or non-critical errors like a failed array, error settings are set to high, etc. I would check inside of the included files to see if they are running any code that might display any output or errors. Also (and this isn't recommended for development) but you could try setting the error_reporting (php.ini) level to display NO errors. That would at least tell you that you're inadvertantly sending output to the browser via some failed function. Quote Link to comment Share on other sites More sharing options...
advancedfuture Posted June 24, 2008 Share Posted June 24, 2008 Here this will solve your problem At very top of the page enter <?php ob_start(); ?> and at the very bottom of the page put <?php $output = ob_get_contents(); ob_end_clean(); echo $output; ?> This will cache the whole page into the $output variable before the headers are sent out. It will eliminate your error. Quote Link to comment Share on other sites More sharing options...
sdguy Posted June 24, 2008 Author Share Posted June 24, 2008 Worked like a charm AF, thanks! Do you have any idea why it was working for the guy in the tutorial without output buffering? Just curious, has output buffering been linked to any other probs that will throw me for a loop later? Quote Link to comment Share on other sites More sharing options...
trq Posted June 24, 2008 Share Posted June 24, 2008 Just curious, has output buffering been linked to any other probs that will throw me for a loop later? Not really, but its a pretty dirty hack when used to simply fix poor code. There is no point in attempting to output anything to a brwoser prior to redirecting as it won't be seen once the redirect takes place. Fix your code. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 24, 2008 Share Posted June 24, 2008 To get help with the actual error, you would need to post the actual code mentioned in the error - (output started at C:\Program Files\xampp\htdocs\widget_corp\create_subject.php:16) If the code you did post is from create_subject.php, then it is not the whole file. There is a blank line at line #8, between one of the closing php ?> tags and the following opening <?php tag that is probably the actual line 16 that the error mentions. This blank line that is outside of the php code tags is content that is being sent to the browser. If this blank line is not present in the tutorial, then it is the cause of the problem. If the blank line in the code you posted is in the tutorial's code, then it is likely that output buffering is turned on in php.ini on the author's system. Quote Link to comment Share on other sites More sharing options...
sdguy Posted June 24, 2008 Author Share Posted June 24, 2008 PFM, you are indeed the PHP Help Guru. I was so focused on not having any spaces before the initial php that I forgot that since the entire page is just php code blocks that any spaces between code would be sent to the browser and mess up my header redirect (I think that's right). I learned a little about output buffering and fixed the code the right way, thanks all. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.