如果未登录,如何让Apache返回404?
在我的网站上,/index.php文件是一个登录表单。我想将Apache或PHP配置为在用户未登录的情况下,对除/index.PHP以外的任何文件返回404错误(而不是403,因此服务器上的文件不明显)。我该怎么做?返回404头很简单:如果未登录,如何让Apache返回404?,apache,php,http-status-code-404,Apache,Php,Http Status Code 404,在我的网站上,/index.php文件是一个登录表单。我想将Apache或PHP配置为在用户未登录的情况下,对除/index.PHP以外的任何文件返回404错误(而不是403,因此服务器上的文件不明显)。我该怎么做?返回404头很简单: <?php if($loggedIn == false){ header("HTTP/1.0 404 Not Found"); } else{ //some other code } ?> 问题在于,将标题更改为404不会
<?php
if($loggedIn == false){
header("HTTP/1.0 404 Not Found");
} else{
//some other code
}
?>
问题在于,将标题更改为404不会显示404错误页面,而只会显示标题设置为404的空白页面(搜索机器人不会为页面编制索引,但访问该站点的用户将不知道发生了什么)。您必须自己添加404内容
例如:
<?php
if($loggedIn == False){
header("HTTP/1.0 404 Not Found");
?>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL <?=$_SERVER['PHP_SELF']?> was not found on this server.</p>
<hr>
<address>Apache/x.x.xx (Debian) Server at xxx.xxx.xxx.xxx Port x</address>
</body>
</html>
<?php
exit;
} else {
//some other code here
}
?>
404找不到
找不到
在此服务器上找不到请求的URL
位于xxx.xxx.xxx.xxx端口x的Apache/x.x.xx(Debian)服务器
如答案所示,更改标题很容易。但是,使用正确的401/403响应不需要泄漏有关服务器上的文件的任何信息。
if (!$logged_in)){
header('HTTP/1.0 404 Not Found');
}