Activate 404 page with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Activate 404 page with php

Post 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();
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Activate 404 page with php

Post 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?
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: Activate 404 page with php

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Activate 404 page with php

Post 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.
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: Activate 404 page with php

Post by Quazel »

I knew how to do that but I though their might be a better way, thanks for your help.
Post Reply