Sending 404 error headers

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
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Sending 404 error headers

Post by seodevhead »

I have my script set up so that if a wrong parameter is sent in the URL, a 404 error header is sent to the client and the script exitted.

Code: Select all

if (..wrong param..)
{
      header("HTTP/1.0 404 Not Found");
      exit();
}
It seems to be working fine, except that when accessing this page (the url with the wrong param), it is coming up as a blank page, not the usual 404 error page I have setup on my server... you know, the standard:

Not Found
The requested URL /nosuchpage.php?id=2 was not found on this server.

Any idea how I can get this page to display? As far as search engines are concerned, is what I have done the same as if the file never existed? Thanks!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Of course it is ;) The 404 error header is merely a status returned by the server. You should create some content to send after the header. Apache or IIS usually do this themselves but in this case you're looking to make your own custom 404 page.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Code: Select all

function send_404()
{
	header('HTTP/1.x 404 Not Found');
	print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'."\n".
	'<html><head>'."\n".
	'<title>404 Not Found</title>'."\n".
	'</head><body>'."\n".
	'<h1>Not Found</h1>'."\n".
	'<p>The requested URL '.
	str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
	' was not found on this server.</p>'."\n".
	'</body></html>'."\n";
	exit;
}
That's a pretty standard Apache print out.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

You could also create a .htaccess-file with this data:

Code: Select all

ErrorDocument 404 /404.php
And then make a 404.php file with all information you want to be viewed.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

NiGHTFiRE wrote:You could also create a .htaccess-file with this data:

Code: Select all

ErrorDocument 404 /404.php
And then make a 404.php file with all information you want to be viewed.
How's that going to help you exit out of a PHP file?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Well you make a 404.php and make all your php code in that file?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

NiGHTFiRE wrote:Well you make a 404.php and make all your php code in that file?
No you've misunderstood why this was needed ;)

It's not a "real" 404 page.. It's spoofed so to speak. Using apache means that you'll only see the 404 page if there really isn't a document there.
Post Reply