Forum Moderators: coopster
I'm a bit of a PHP rookie. I am having some trouble with a script that I use to put together small sites dynamically.
In the main template I use an array of 'valid variables' so that the variable in the url is looked up and confirmed before the include is... well, included (there must be a technical term for this). If the variable is not in the list, an 'else' statement passes an 'error' include.
Problem
The problem is that when passed through to the error include, it gives a '200' header response. This is bad news.
I'm sure this can be done, and I searched, but is there a way that I can check the variable, and if not there serve up the real 404 error page i.e. forward or redirect to a static error page?
if you enclose the whole of the page with
<?php
ob_start();
// all page content here
// including headers and html in any order
ob_end_flush();
?>
then php will save all the page output in a buffer and send it all at once to the browser with the headers in the correct order (regardless of where they appear in the code)
Problem is that I have my doctype, html declaration, and head all set up in the template.
Here's what I have:
if ( in_array( blah blah) )
{
include("{$variable}.php");
}
else
{
// 404 page
include( "error.php" );
}
?>
Is there some way that instead of else -> include I just say else (goto error page)? Here, the error page would be a static php page where I could put the headers.
There may be a simple way to do this with PHP, but , to put it simply, 'I can only really honk the horn of PHP, I don't know enough to drive yet'.
If there was already some output before the if/else then the headers were already sent making it too late to modify them. All that's left is whatever you can do with HTML, which isn't much unfortunately.
Silly me. There wasn't any prior output.
if (!in_array( blah blah))
{
header("HTTP/1.1 404 Not Found");
header("Status 404 Not Found");
header("I love: Vincevincevince");
}
Thanks vince^3 - this works. Hopefully now Google can clean up my sites listings, provided it doesn't mind getting the 'I love: vincevincevince' header.