Forum Moderators: coopster

Message Too Old, No Replies

404 header from include file

404 header from include file

         

maxi million

6:28 pm on Sep 17, 2005 (gmt 0)

10+ Year Member




my program takes two parameters $_REQUEST['cat'] and $_REQUEST['subcat'] and based on the first, includes a relevent file and based on the second fetches some data from the db. this happens in a file called genericbody.php which is again included in index.php along with some other common files.

i have functions which deal effectively with this architecture. but my problem is how do i send a 404 header if say, the file requested through $_REQUEST['cat'] does not exist or even if it does the id requested thru $_REQUEST['subcat'] does not exist in the related tables?

i tested with both header("HTTP/1.0 404 Not Found"); and header("Status: 404 Not Found"); for the purpose, set up a customised 404.php page. yet nothing happens. i mean theres no errors (on my production environment i have switched off error reporting and in my dev environment i dont get any errors either). its all the more frustrating coz all other kind of headers work. i start ob_start at the top of my pages for these kinds of things and yet i dont get what i want.

i know i can create other error pages to take care of these problems, but i want the 404 haeder to work basically.

how do i resolve this issue? any thoughts?

could the problem be happening coz the header is being sent from include files from outside my www directory?

any thoughts anyone?please

grandpa

9:46 am on Sep 18, 2005 (gmt 0)

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



What do you mean when you say yet nothing happens.?

Is your custom 404 page getting displayed, but with a different status (not 404, but 200 OK)?

Is the custom 404 page getting displayed at all? If not, what then?

maxi million

6:08 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



what happens is:
my common header files and common footer files get include. so the ultimate product is a merged (if i may say so) html output of the header and footer and there is nothing in the middle. there is no redirection and no errors.

jd01

7:36 pm on Sep 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you have tried the correct setting of the header, header("HTTP/1.0 404 Not Found"); could you post where this is in relation to the other headers you are serving? Is it first? Is it a conditional -- IOW are other headers being sent even if there is no information found?

The header() code is correct, so it looks like it comes down to code order/structure/conditional value(s).

Justin

maxi million

10:10 am on Sep 19, 2005 (gmt 0)

10+ Year Member



exactly thats the case...other headers are being sent even if theres no info to be displayed. the output is...there is nothing where there should be included/db fetched content...surrounded on top by the header file and at the bottom by the footer file.

this 404 header is indeed conditional...send it if there is nothing.

are other headers being sent even if there is no information found?
Yes actually. since other parts of the headers are created in various included files. the decision whether or not 404 is to be sent occurs in the middle segment.

btw there is not likely to be any conflict between two separate headers going out together. the header to be sent is declared as a config variable available throughout the site and its just that variable which is repeated everywhere.

jd01

7:27 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you run a header check [webmasterworld.com]? If you do not exit(), or take some other action after setting/sending the 404, the results you described are expected.

If you run the header check and you are not getting a 404...

I would guess the problem has to do with the other headers -- I would set them in the includes as you are doing, but not send them until after the decision is made to send a 404 or not. IOW Run your scripts, but do not output any information or send any headers until you know if there is a result or not. Then send them all at once.

If you do this, make sure you do not set another status code after the 404, because it will override the initial status. (If you are redirecting when you send 'not found', I would recommend using an include instead.)

Let us know what you find.

Justin

Romeo

9:36 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



are other headers being sent even if there is no information found?
Yes actually. since other parts of the headers are created in various included files. the decision whether or not 404 is to be sent occurs in the middle segment.

It is not quite sure what you mean with "other parts of the headers". HTTP headers? HTML page headers?

If you want to state a 404, just send the plain one 404 HTTP header() and nothing else.
The header() stuff would not work at all, if you have output any text before (including any HTML header text), so your decision to send a 404 or not has to be at the very beginning and before of all further page generation.

Regards,
R.

maxi million

3:33 am on Sep 20, 2005 (gmt 0)

10+ Year Member




I have run the header check. this is what i got:

HTTP/1.1 404 Not Found
Date: Tue, 20 Sep 2005 03:21:58 GMT
Server: Apache
X-Powered-By: PHP/4.3.11
Set-Cookie: BKiD=191865409; expires=Thu, 20-Oct-05 03:21:58 GMT; path=/; domain=.deletedthedomainname.com
Connection: close
Content-Type: text/html

as expected its returning HTTP/1.1 404 Not Found, for a cat and subcat which do not exist.

so as jd01 mentioned, i might need to exit(); will try that.

"IOW Run your scripts, but do not output any information..."
I already have ob_start(), is there any other ways this can be achieved?

To answer Romeo's question: they are HTML page headers. theres nothing else i am sending thru the php header();

thank you jd and romeo for all the help. and particularly this Server Header Check tool is very useful