从PHP访问apache errordocument指令

ala*_*and 4 php apache

如果不满足某些条件,我想将php脚本输出像真正的404页面(在Apache ErrorDocument指令中设置).我不知道我怎么能/如果可以从PHP访问这个值..

if(!@$_SESSION['value']){
 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
 echo $default_page['404'];
 exit();
}
echo 'Welcome to a secret place.';
Run Code Online (Sandbox Code Playgroud)

我理解ErrorDocument值可以被覆盖,但我对Apache硬编码的'default'值特别感兴趣.如果可以知道被覆盖的值(例如通过.htaccess文件),那么这是一个奖励:)

http://httpd.apache.org/docs/2.0/mod/core.html#ErrorDocument

编辑:要清楚,我想从PHP发送内容默认的404页面(或403等).如果我只使用header它自己,则没有任何内容输出到客户端/用户(至少在FF/Chrome中,IE有自己的内置页面显示).

RoU*_*oUS 6

建议从PHP设置响应代码的方法是@mario建议的:

Header('Status: 404 Not Found');
Run Code Online (Sandbox Code Playgroud)

如果你想获得服务器通常提供的页面主体404,并且不关心在用户的浏览器中重写的URL,你可以使用类似的东西:

$uri_404 = 'http://'
    . $_SERVER['HTTP_HOST']
    . ($_SERVER['HTTP_PORT'] ? (':' . $_SERVER['HTTP_PORT']) : '')
    . '/was-nowhere-to-be-seen';
Header("Location: $uri");
Run Code Online (Sandbox Code Playgroud)

(当你直接擦除Location标题字段时,你需要提供一个完整的URI.)结果是用户的浏览器最终会指向那个虚假的位置,这可能不是你想要的.(可能不是.)那么你可以自己收集页面的内容并将两者结合起来,实际上:

Header('Status: 404 Not Found');

$uri_404 = 'http://'
    . $_SERVER['HTTP_HOST']
    . ($_SERVER['HTTP_PORT'] ? (':' . $_SERVER['HTTP_PORT']) : '')
    . '/was-nowhere-to-be-seen';
$curl_req = curl_init($uri);
curl_setopt($curl_req, CURLOPT_MUTE, true);
$body = curl_exec($curl_req);
print $body;
curl_close($curl_req);
Run Code Online (Sandbox Code Playgroud)

这应该获取服务器为虚假URI报告的404页面的内容,然后您可以复制它并自己使用它.这应该正确处理任何ErrorDocument 404处理程序输出.