Handling 404 errors correctly
3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 15, 2013 (RSS)
By gversion - May 15, 2013
Hello,
When a listing has been deleted from my website I would like to return a 404 error with the correct HTTP Response Header.
I am aware of the following two methods but neither are doing exactly what I require:
if (!$Record) { dieWith404("Record not found!"); } // show error message if no record found
The above code seems to return the correct HTTP Response Header but I would like it to forward to users to my custom 404 error page rather than display an error message.
Therefore I have tried using the code below to redirect the user:
if (!$Record) {
redirectBrowserToURL("error.html");
exit;
} // redirect user if no record found
This code works but displays the header "HTTP/1.1 302 Moved Temporarily", which isn't correct.
Could someone please explain how I can combine the two so that I am able to forward users to my custom error page and send a 404 HTTP Repsonse Header?
Thank you,
Greg
By gregThomas - May 15, 2013
Hi Greg,
I've done a bit of research, and this method seems to work quite well:
if (!$Record) {
dieWith404(file_get_contents('error.html'));
}
I had to use the file_get_contents function as opposed to include as the echo in the dieWith404 causes a trailing 1 to appear at the end of the file.
Let me know if you have any questions.
Cheers!
Greg
PHP Programmer - interactivetools.com