Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php 404 Not Found Header

Tags:

php

header

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.

like image 853
Aviel Fedida Avatar asked Apr 27 '13 16:04

Aviel Fedida


1 Answers

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

like image 166
Lukas Greso Avatar answered Sep 29 '22 03:09

Lukas Greso