首页 > 解决方案 > HTML 文件的 PHP file_get_contents 请求旧资源

问题描述

我有一个特定的网页。这是文件结构:

index.php
script.js
style.css
edit
 |
 |--- index.php
 |
 |--- script.js

edit/index.php正在回显index.phpusing file_get_contents('../index.php')which contains的内容<script src='script.js'></script>edit/script.js包含alert(). 当我打开https://www.example.com/edit时,它显示的内容index.php,但它不执行里面的代码edit/script.js。为什么会发生这种情况,我该如何解决?(虽然index.php有扩展.php它不包含任何 PHP,只是 HTML)

标签: javascriptphpfile-get-contents

解决方案


问题是浏览器不知道它edit实际上是edit/index.php. 它认为这是根目录中的文件,因此在解析时script.js也会在根目录中查找该文件。

您应该配置网络服务器,以便它执行从edit到的重定向edit/index.php,而不仅仅是返回 的内容edit/index.php。这样,所有相对 URL 都将被正确处理。如果您不想显示脚本名称,您可以重定向到edit/-- 这足以让它知道这是一个目录而不是文件。


推荐阅读