Jump to content

[SOLVED] header("Location: www.whatever.com/") redirect probs


sdguy

Recommended Posts

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.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.