8

I am trying to understand the combination of 3 simple php code lines, This is the code:

ob_end_clean();
header('HTTP/1.0 404 Not Found');
exit;

So this is the code and as i understand the first line ob_end_clean();, Can help for example with BOM(Byte order mark), So the first line is to prevent any previous output.

The second line header('HTTP/1.0 404 Not Found'); is the header.

And the third line exit terminates the script execution.

If i remove the first line and i got a BOM at the document i get blank page (No 404).

If i remove the third line (with and without the BOM), I get the page i wanted no blank page and no 404.

  • Mabye if anyone can explain why should i use the exit After the 404 header
  • Also why with the BOM i dont get "headers already sent error"

Thank you all and have a nice day.

Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
  • possible duplicate of [Why won't my PHP app send a 404 error?](http://stackoverflow.com/questions/437256/why-wont-my-php-app-send-a-404-error) – Arend Apr 02 '14 at 13:35

2 Answers2

11

If i remove the first line and i got a BOM at the document i get blank page (No 404). you get blank 404 because you have no content defined in there...

header('HTTP/1.0 404 Not Found');

is only giving notice that user is on 404 error page site... if you want to display some 404 notice for user you can do this by loading your 404.html file

if(strstr($_SERVER['REQUEST_URI'],'index.php')){
  header('HTTP/1.0 404 Not Found');
  readfile('404missing.html');
  exit();
}

or directly

if (strstr($_SERVER['REQUEST_URI'],'index.php')){
    header('HTTP/1.0 404 Not Found');
    echo "<h1>Error 404 Not Found</h1>";
    echo "The page that you have requested could not be found.";
    exit();
}

exit function is there because you have to prevent execution of another php code, which may be directly after if or which may be excecuted later, simply it says END

Lukas Greso
  • 472
  • 4
  • 6
  • Hey Lukas as i understand your if statement is for the code work only on index pages. – Aviel Fedida Apr 27 '13 at 16:44
  • No, it's apache definition of 404 page... I don't know what's the situation of your code, you can use only content of `IF` statement. Notice: header is message for 'robots', 'crawlers' and echo is message for 'humans' ... – Lukas Greso Apr 27 '13 at 17:03
  • Thank you for your answer Lukas, Can you tell me please why the browser parse and shows the 404 only if i kill the rest of the script? – Aviel Fedida Apr 27 '13 at 17:10
  • Also i tryed to understand your if statement, And as i say i tested and it return index.php string, Also what do you mean when you say "you can use only content of IF statement", Thank you and have a nice day. – Aviel Fedida Apr 27 '13 at 17:27
  • it seems that your php is still in process, php will not print output until it finished execution (reach end of php file). You have to stop it manualy by exit. If you want to execute some php on 404 page.. do it before sending `head` and `echo`.. Execution of basic php may take miliseconds, user will not notice that something was processed before he got 404 msg. – Lukas Greso Apr 27 '13 at 17:33
  • content of 'IF' means that you can simply use `header('HTTP/1.0 404 Not Found'); echo "

    Error 404 Not Found

    "; echo "The page that you have requested could not be found."; exit(); `
    – Lukas Greso Apr 27 '13 at 17:34
  • So as i understand the event sequence is as following: Php define a header, The browser get response with the 404 header(I assume that mabye for few miliseconds the browser does show the 404 error), But then becouse i didnt exit; php send the rest of the file that replace the 404 error, Am i right? – Aviel Fedida Apr 27 '13 at 17:56
1

why should i use the exit After the 404 header

So that no further code will be executed. If there isn't any then, fine, it isn't necessary in this case. It's a good habit to get into though.

Also why with the BOM i dont get "headers already sent error"

You didn't configure your PHP installation to show errors and notices to the end user.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you for your answer, About the 404 header if you can please tell me why only when i kill the rest off the code with (exit), Why only then i get the 404 what does the server return to the browser. And about the "headers already send" you are right. – Aviel Fedida Apr 27 '13 at 16:35
  • I cant understand why the browser will read the 404 header only if i exit the script. – Aviel Fedida Apr 27 '13 at 16:52