我正在尝试将PHP文件包含在另一个目录中。
这是我的文件结构
settings\
manage.php
website \
index.php
main.js
main.css
对于manage.php
echo 'Welcome to the manager! Here is your website:';
include('../website/index.php');
的index.php
<script src='main.js'></script>
<link rel="stylesheet" type="text/css" href="main.css">
所以,当我加载manage.php时,我没有得到main.js或main.css。我怎样才能让它发挥作用?也许包括不是正确的方法去?我无法修改网站文件夹中的任何内容。
[我知道iFraming是一个可能的解决方案,但我希望得到另一个答案]
答案 0 :(得分:3)
由于您无法编辑/website/
内容,因此您应该尝试使用这个丑陋的代码来启动。
在manage.php
之前的include
语句之前添加以下内容
echo('<base href="../website/">');
如果它对您有用,那么您可以考虑在包含html文件之前使用PHP发送正确的标头,而不是直接回显基本标记。
请将 jeroen 的评论视为实际解决方案并使用框架
答案 1 :(得分:2)
此处的问题是,当浏览器加载/settings/manage.php
时,它会向/settings/main.js
和/settings/main.css
提出不存在的请求
您可能需要将index.php中的html更改为以下内容:
<script src="/website/main.js"></script>
<link rel="stylesheet" type="text/css" href="/website/main.css">
注意我已根据您的目录布局对您的网址做了一些假设,因此您可能需要调整我的解决方案以使其适合您
答案 2 :(得分:1)
基于以下问题的评论:如果您想为您的用户添加一些功能/代码(而不是相反;您在代码中包含用户的内容),您应该查看auto_prepend_file
指令。
基本上,您在php.ini文件中指定要在主文件之前添加(作为require
)某个php文件。
编辑:由于您无法访问php.ini,但可以使用.htaccess
文件,因此可以将其放入.htaccess
:
php_value auto_prepend_file "/path/to/your/file.php"