Pergunta

I have read a tutorial, and in that tutorial they make a error function.
When the function was called it runs:

<?php
    header('HTTP/1.0 404 Not Found');
    include('errorpage.php');
    exit();
?> 

But what is the use of that? Why can't you use header('Location: errorpage.php') or skip the part header('HTTP/1.0 404 Not Found')?

Foi útil?

Solução

If you use header('Location: errorpage.php') then you are saying "The document you want can be found here". This is a lie.

If you don't include header('HTTP/1.0 404 Not Found') then you are saying "OK, here is the document you asked for" and then displaying an error message. This is a lie.

If the error page is for an attempt from a browser to run a JavaScript, it will try to execute the HTML as JS and throw an error.

If the client is a search engine, it will index the error page as content instead of treating the link as broken. This will give you bad results in searches.

If the client is a downloading tool, it will download the document as a file. If it is going recursively, it could end up following vast numbers of links to error pages on your server and fill up the user's hard disk while eating lots of your bandwidth.

And so on.

In short, if you don't tell the client it is an error then it will treat it as content.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top