Adding 404 Server Header Via Php
Hi,
Anyone know how to change the below code so that any pages that returned the system generated 404 page would also return a 404 "not found" server header?
$error404 = '<p style="font-size:15px;font-weight:bold;">Error 404 - page does not exist</p><p>Contact the <a href="mailto:'.$row_site_control['mailto'].'">systems administrator</a> to report this</p>';
Thanks.
pr0gr4mm3r posted this at 16:45 — 1st August 2010.
He has: 1,502 posts
Joined: Sep 2006
This code should work for you:
<?php
header("HTTP/1.0 404 Not Found");
?>
NickD posted this at 17:46 — 1st August 2010.
They have: 56 posts
Joined: May 2009
Hi programmer,
How is the above integrated into
$error404 = '<p style="font-size:15px;font-weight:bold;">Error 404 - page does not exist</p><p>Contact the <a href="mailto:'.$row_site_control['mailto'].'">systems administrator</a> to report this</p>';
so that the 404 server header only affects the 404 page?
NickD posted this at 17:57 — 1st August 2010.
They have: 56 posts
Joined: May 2009
Want me to post the whole header file?
Renegade posted this at 21:52 — 1st August 2010.
He has: 3,022 posts
Joined: Oct 2002
Headers usually go at the top of the file and have to be declared before any output.
You can read more about it on the header function manual page:
http://nz2.php.net/manual/en/function.header.php
NickD posted this at 10:24 — 2nd August 2010.
They have: 56 posts
Joined: May 2009
Yes, I know that,
My problem is that the header is included in all pages, with just one line which specifys the content for a server error (404) (code shown above), It therefore looks like its going to be quite complicated to achieve as I cant just 404 the main header because it will return a not found header for the entire site.
pr0gr4mm3r posted this at 01:22 — 17th August 2010.
He has: 1,502 posts
Joined: Sep 2006
That function sends the 404 message in the page HTTP headers, not to be confused with your header template if you have one.
My header() function can go anywhere in your code as long as you haven't sent anything to the browser yet. If there is no way to call the header() statement before anything is printed, you may want to look into output buffering.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.