Php 404 Not Found Header

I am trying to understand a combination of three simple lines of php code. This is the code:

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

So, this is the code, and as I understand it, the first line is ob_end_clean (); . It can help, for example, using the specification (bytes). . So the first line is to prevent any previous exit.

The second header ('HTTP / 1.0 404 Not Found'); - heading.

And the third line exit completes the execution of the script.

If I delete the first line and I have the specification in the document, I get a blank page (No. 404).

If I delete the third line ( with and without specification ), I get a page on which I do not need a blank page, not 404.

  • Mabye, if anyone can explain why I should use exit . 404
  • Also why with the spec I don't get "headers have already sent an error"

Thank you all and have a nice day.

+6
source share
2 answers

If I delete the first line and I have the specification in the document, I get a blank page (No. 404). you will get an empty 404 because you do not have the content defined there ...

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

only reports that the user is on the page of the 404 error page ... if you want to display a notification for the 404 user, you can do this by downloading the 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

exists because you need to prevent the execution of other php code, which can be immediately after if or which can be deleted later, it just says END

+10
source

Why should I use output After heading 404

Thus, no further code will be executed. If not, then in this case it is not necessary. It's a good habit to come in though.

Also why with BOM I do not get "headers already sent errors"

You have not configured your PHP installation to show end users errors and notifications.

+1
source

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


All Articles