Page 2 of 2

Posted: Thu Nov 16, 2006 8:36 pm
by Mr Tech
Why is it bad anyway? Does it effect the PHP script or is it just dirty coding?

Posted: Thu Nov 16, 2006 8:48 pm
by Luke
It's kind of like putting newspaper over a pile of dog crap instead of cleaning it up... you may not see the problem any more but it's still there.

This is why the majority of experienced developers seperate applications into layers. You should be doing all of the application logic long before any output is sent... if you did, you wouldn't have to worry about the "headers already sent" problem at all...

Code: Select all

<?php
// All application logic goes here
$foo = santitize($_GET['input_var']);
$foo = do_some_stuff_to($foo);

// If we want to do a redirect, do it before output is send...
if($foo == false){
    // No error here, since there hasn't been any output yet...
    header('Location: http://www.someplace.com');
    exit;
}

// OK, now that all of our logic is done... now we can send output...
echo $foo;
?>
Follow the link feyd posted... there's all kinds of info about this.

Posted: Thu Nov 16, 2006 9:28 pm
by Mr Tech
My problem is because I include the header layout of the script at the top, of the page... Like this:

Code: Select all

<?php
incluide("headertemplate.html");

if ($foo == false) {
header("location: blah.php");
} else {
echo "this is the text...";
}

incluide("footertemplate.html");
?>
Would I have to do it like this?

Code: Select all

<?php
if ($foo == false) {
header("location: blah.php");
} else {
$content = "this is the text...";
}

incluide("headertemplate.html");
echo $content;
incluide("footertemplate.html");
?>
Thanks for your help.

Posted: Thu Nov 16, 2006 9:34 pm
by Luke
Yup... pretty much.