Loading a PHP page progressively...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Why is it bad anyway? Does it effect the PHP script or is it just dirty coding?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Yup... pretty much.
Post Reply