从PHP访问Apache的ErrorDocument指令 [英] Access apache errordocument directive from PHP

查看:219
本文介绍了从PHP访问Apache的ErrorDocument指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出像一个真正的404页,一个PHP脚本输出(如Apache的ErrorDocument指令集),如果某些条件不具备。我不知道我怎么能/是否有可能从PHP访问此值。

I would like to make a php script output like a real 404 page (as set in the Apache ErrorDocument directive) if certain conditions are not met. I'm not sure how I can / if it's possible to access this value from PHP..

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

我理解的ErrorDocument值可以被覆盖,但我通过Apache的默认值硬codeD特别感兴趣。如果有可能知道哪些是覆写(例如,通过.htaccess文件)中的值,那么这是一个奖金:)

I understand the ErrorDocument value can be overwritten, but I'm particularly interested in the 'default' value hardcoded by Apache. If it's possible to know the value which is overwitten (eg by a .htaccess file), then that's a bonus :)

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

编辑:要清楚,我想将内容从PHP发送默认的404页(或403等)的Apache。如果我只用对自己那么没有什么是输出到客户端/用户(至少在FF /铬,IE浏览器有自己内置的文档,它显示)。

edit: to be clear, I'd like to send the content the default 404 page (or 403, etc) of Apache from PHP. If I only use header on its own then nothing is output to the client/user (at least in FF/Chrome, IE has its own built in pages which are shown).

推荐答案

设置从PHP的响应code推荐的方法是为@mario建议:

The recommended way to set the response code from PHP is as @mario suggested:

Header('Status: 404 Not Found');

如果你想获得该页面的服务器通常会提供了404的身上,不关心在用户的浏览器的URL重写得到,你可以使用类似:

If you want to get the body of the page the server would ordinarily provide for a 404, and don't care about the URL getting rewritten in the user's browser, you could use something like:

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

(当你直接frobbing的位置报头字段,你需要提供一个完整的URI)。这样做的结果是,用户的浏览器的最终会指向该伪造的位置,你想要什么了不得。 (可能不是)。于是,你可以实际工作收集自己页面的内容,二者结合起来,有效:

(When you're directly frobbing the Location header field, you need to supply a full URI.) The result of this is that the user's browser's will end up pointing to that bogus location, which may not be what you want. (Probably isn't.) So then you could actuall collect the contents of the page yourself and combine the two, in effect:

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);

这应该取404页的内容,以伪造的URI的服务器报告,然后你就可以将它复制并自己使用它。这应该妥善处理任何的ErrorDocument 404 处理输出。

That should fetch the contents of the 404 page the server reports for the bogus URI, and you can then copy it and use it yourself. This should properly handle any ErrorDocument 404 handler output.

这篇关于从PHP访问Apache的ErrorDocument指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆