views:

104

answers:

3

I'm trying to build my own MVC as a practice and learning experience. So far, this is what I have (index.php):

<?php
require "config.php";

$page = $_GET['page'];
if( isset( $page ) ) { 
    if( file_exists( MVCROOT . "/$page.php" ) ) {
        include "$page.php";
    } else {
        header("HTTP/1.0 404 Not Found");
    }
}


?>

My problem here is, I can't use header to send to a 404 because the headers have been sent already. Should I just redirect to a 404.html or is there a better way? Feel free to critique what I have so far (it's very little). I would love suggestions and ideas. Thanks!

+3  A: 

Standard practice in MVC frameworks is to use output buffering (ob_start(), ob_get_contents(), and ob_end_clean()) to control how, when, and what gets sent to the user.

This way, as long as you capture your framework's output, it doesn't get sent to the user until you want it to.

To load the 404, you would use (for example):

<?php
require "config.php";

$page = $_GET['page'];
ob_start();

if (isset($page)) {
    echo "isset is true";
    if (file_exists(MVCROOT."/$page.php")) {
        include MVCROOT."/$page.php";
        $output = ob_get_contents();
        ob_end_clean();
        echo $output;
    } else {
        ob_end_clean(); //we don't care what was there
        header("HTTP/1.0 404 Not Found");
        include MVCROOT."/error_404.php"; // or echo a message, etc, etc
    }
}
?>

Hope that helps.

Austin Hyde
Thanks, I like this!
Doug
I'm expecting some 404 message, but it's just a blank page on Firefox. It says broken on other browsers.
Doug
A lot of times, if there is no content along with the response code, then the browser will supply their own. Try adding your own 404 page after the header. I'll update my answer.
Austin Hyde
+1  A: 

Im not very good at english but i will try; the 404 error trigers on the server before running any code (because is suposse that the page does not exist, so there is no code).

So, if you want to give the user a 404 error seeking for the error in a php code, you must use a simple redirection to a 404.html.

In other hand, if you have access to the server config files you can program this on the server instead of a web page running on it. You can use WAMP to practice...

I hope you understand me. Cyaa

EDIT i must add:

$page = $_GET['page'];

This will give you an error if $_GET['page'] is not set, you MUST check for isset($_GET['page']) before trying using it.

DomingoSL
Don't exactly agree with the edit part.
Doug
@Doug, it won't give you an error, but it will give a warning. You should do something like `$page = isset($_GET['page'])?$_GET['page']:null;` That way, no warnings are generated, and you know the exact value of `$page` in the event `$_GET['page']` is not set.
Austin Hyde
I actually never knew it generated an error. I see it now. I stand corrected. Thanks for showing me that guys!
Doug
thats right, not an error, a warning... still improving my english :)
DomingoSL
A: 

You should either redirect or simply include it, here is modified code:

require "config.php";

$page = $_GET['page'];
if( isset( $page ) ) { 
    echo "isset is true";
    if( file_exists( MVCROOT . "/$page.php" ) ) {
        include  MVCROOT . "$page.php";
    } else {
        include  MVCROOT . "404.html";
    }
}
Sarfraz
That's just including a page telling a user about the 404 error, with the actual HTTP 404 header totally gone.
BoltClock
@BoltClock: Yes that's true, it is basically to this sentence of the OP **Should I just redirect to a 404.html**
Sarfraz