PHP, MVC, 404 - How can I redirect to 404?

I am trying to create my own MVC as experience and 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 is here, I cannot use the header to send to 404 because the headers are already sent. Should I just redirect to 404.htmlor is there a better way? Feel free to criticize what I have so far (this is very small). I would like suggestions and ideas. Thank!

+3
source share
3 answers

MVC (ob_start(), ob_get_contents() ob_end_clean()), , , .

, , , , .

404, ():

<?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
    }
}
?>

, .

+6

, ; 404 - ( , , ).

, 404, php-, 404.html.

, , , -, . WAMP ...

, . CyaA

EDIT :

$page = $_GET ['page'];

, $_GET ['page'] , isset ($ _ GET ['page']), .

+1

You must either redirect or simply enable it, the code has been changed here:

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";
    }
}
0
source

Source: https://habr.com/ru/post/1750217/


All Articles