【发布时间】:2016-08-03 20:45:51
【问题描述】:
我有一个好奇心。如果 PHP 脚本包含自身会发生什么?
我问的原因是我有一个 php 文件,它为我网站的所有 <head> 标签生成内容。这包括网站的 404 error page head 标签。
如果直接访问生成这些head 标记的php 文件,则通过包含404 error page 来“假装”不存在,这会将404 status 发送回客户端。
这是文件系统的样子:
/
.htaccess <- (apache 404 handling)
404.php <- 404 error page
head.php <- '<head>' generator
...
404.php 包含:
<?php
header("HTTP/1.0 404 Not Found");
?>
<html>
<head>
<?php
$key = true;
include dirname(__FILE__) . "/head.php";
?>
</head>
<body>
lul. dead.
</body>
</html>
head.php 包含:
<?php
if(isset($key) && $key===true){
//code to generate <head> content
}else{
include dirname(__FILE__) . "/404.php";
}
?>
$key 告诉head.php 它包含在另一个 php 文件中,而不是通过 URL 请求。
所以,总而言之,如果我直接从我的网络浏览器请求head.php(不是通过php includes 访问它,只是通过http 请求)它应该:
(a) 意识到请求不是来自另一个 php 文件的 include,然后
(b) 包含 404.php,其中
(c) 在404.php 的line 2 上发送404 status,然后,
(d) 包括head.php。
因此,最终head.php 将包含head.php。
会发生这种情况吗?
【问题讨论】:
-
我不确定这是否可行,所以我将自己的结果发布到堆栈上,以便其他人看到。