Forum Moderators: coopster

Message Too Old, No Replies

'Forwarding' to a 404 page

Need help with an 'if else'

         

mipapage

10:14 am on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay,

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.



Situation
Basically I have one include for each page that contains functions to put a page together into a main template.

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?

jamesa

12:57 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd have to explicitly output a 404 header. Headers need to be sent before any other output (like text or even white spaces). This page [us4.php.net] of the PHP manual actually has an example of sending a 404 header.

mipapage

4:11 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamesa,

Thanks. Off to give it a try - looks like just what I needed. :-]

vincevincevince

4:36 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




else
{
header("HTTP/1.1 404 Not Found");
header("Status 404 Not Found");
header("I love: Vincevincevince");
}

Should do the trick!

mipapage

5:02 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right vince^3,

problem is that the way I have my template set up I can't stick that above the <html> tag. I could figure out some way to rework things, but firt I am trying this bit (found in the Google News 404 thread):

ErrorDocument 404 /error.htm

So far no luck. We'll see. Thanks!

jamie

5:26 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mipapage,

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)

mipapage

5:41 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jamie, that's one to remember.

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'.

vincevincevince

6:50 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about putting at the very top of your template:

if (!in_array( blah blah))
{
header("HTTP/1.1 404 Not Found");
header("Status 404 Not Found");
header("I love: Vincevincevince");
}

jamesa

8:36 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

mipapage

9:30 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm... What a crappy system I devised!

Okay, thanks folks. I'll go and have a look at changing things.

Too bad that

ErrorDocument 404 /error.htm

Didn't work...

vincevincevince

10:32 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mm... that's why i said to add that if() at the very very beginning of the page... and leave in place the existing if() as well to do the include

mipapage

12:07 am on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gotcha! So, if it's in the array, send the page, do the include etc. If not, send the 404 header and then fo the include for the error page.

I'll give it a try... tomorrow. Thanks!

mipapage

4:28 am on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.


Thanks again WebmasterWorld!