Jump to content

[SOLVED] Headers (no its not what you think)


yzerman

Recommended Posts

Sample of code:

 

if (defined('constant')) {

  // show content

} else {

  header("HTTP/1.1 404 Not Found");

}

 

ok, what I am trying to do is, use files as includes from my index page where the constant is defined. What I want to happen is if a user trys to goto blah.php directly, instead of /index.php?page=blah they receive a 404 error, and are sent to the 404 file not found page. What I am actually getting is a blank white page. What am i doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/41094-solved-headers-no-its-not-what-you-think/
Share on other sites

This is driving me nuts. I've got the same problem, and I can't figure it out...

 

I found this in the manual, but it didn't work for me. You can try it yourself:

If this doesn't work:

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

 

You should set option cgi.rfc2616_headers in php.ini.

 

If you can't, use this:

<?php
function header_status($status)
{
   // 'cgi', 'cgi-fcgi'
   if (substr(php_sapi_name(), 0, 3) == 'cgi')
       header('Status: '.$status, TRUE);
   else
       header($_SERVER['SERVER_PROTOCOL'].' '.$status);
}
header_status('404 Not Found');
?>

 

See also http://bugs.php.net/bug.php?id=27345

 

 

Orio.

The message that you usually see when there is a 404 error is generated by Apache. If you send the 404 header using PHP then you should also generate an HTML message to notify the user. You can copy the HTML 404 message to give users an identifcal message.

 

If you can, you should also put the include files outside of the "Document Root". If you have them in a directory of their own you should be able to use a .htaccess file to protect the files as well.

 

Order deny,allow
Deny from all

 

 

The message that you usually see when there is a 404 error is generated by Apache. If you send the 404 header using PHP then you should also generate an HTML message to notify the user. You can copy the HTML 404 message to give users an identifcal message.

 

If you can, you should also put the include files outside of the "Document Root". If you have them in a directory of their own you should be able to use a .htaccess file to protect the files as well.

 

Order deny,allow
Deny from all

 

 

Yeah, I use the .htaccess trick alot when I want files available only for php scripts (or ftp).

At least now I understand why I didn't get the 404 when I sent the headers, thanks shoz :)

 

Orio.

The message that you usually see when there is a 404 error is generated by Apache. If you send the 404 header using PHP then you should also generate an HTML message to notify the user. You can copy the HTML 404 message to give users an identifcal message.

 

If you can, you should also put the include files outside of the "Document Root". If you have them in a directory of their own you should be able to use a .htaccess file to protect the files as well.

 

Order deny,allow
Deny from all

 

 

 

Alright, so basically redirecting them to my default 404 page should work then. Appreciate the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.