Adding 404 Server Header Via Php

NickD's picture

They have: 56 posts

Joined: May 2009

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's picture

He has: 1,502 posts

Joined: Sep 2006

This code should work for you:

<?php
    header
("HTTP/1.0 404 Not Found");
?>

NickD's picture

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's picture

They have: 56 posts

Joined: May 2009

Want me to post the whole header file?

Renegade's picture

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's picture

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's picture

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.