Page 1 of 1

Activate 404 page with php

Posted: Sat Oct 09, 2010 9:50 pm
by Quazel
Is it possible to activate the 404 page (as defined in the .htaccess file) with php.
Something like:

Code: Select all

if($this == $that) {
echo "hi";
} else {
active_404();
}

Re: Activate 404 page with php

Posted: Sat Oct 09, 2010 10:04 pm
by requinix
Not really. You can't tell Apache to backtrack and issue a 404 response instead of the regular response it was trying to send (ie, your script). You could try to request a non-existent page on your server and mirror the output and headers but it's more complicated than that. You could try to parse the .htaccess file for the right ErrorDocument but there's an issue with headers as well.

Any particular reason for this, beyond the obvious?

Re: Activate 404 page with php

Posted: Sat Oct 09, 2010 10:09 pm
by Quazel
I'm trying to write a script that gets pages from a sql db if it doesn't exist I want the server to issue a 404 response.

Re: Activate 404 page with php

Posted: Sat Oct 09, 2010 10:14 pm
by requinix
That's easy.

Code: Select all

header("HTTP/1.1 404 Not Found");
You have to generate the page content yourself but with that line of code the response will be an actual 404 page.

Re: Activate 404 page with php

Posted: Sat Oct 09, 2010 10:23 pm
by Quazel
I knew how to do that but I though their might be a better way, thanks for your help.