包括PHP文件'JS, Css


Include PHP file's JS, Css

我试图在另一个目录中包含一个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是一个可能的解决方案,但我希望得到另一个答案]

既然你不能编辑/website/的内容,你应该尝试一下这段丑陋的代码。

在manage.php的include语句前添加以下内容

echo('<base href="../website/">');

如果它对你有效,那么你可以考虑在包含html文件之前用PHP发送一个正确的头,而不是直接回显基标记。

请考虑jeroen的注释作为实际的解决方案,并使用

这里的问题是,当浏览器加载/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">

注意我已经根据你的目录布局对你的url做了一些假设,所以你可能需要调整我的解决方案,使它为你工作

根据问题下面的评论:如果你想为你的用户包含一些函数/代码(而不是其他方式;如果你在代码中包含用户的东西),你应该查看auto_prepend_file指令。

基本上,您可以在php.ini文件中指定要在主文件之前附加(作为require)某个php文件。

Edit:由于您无法访问php.ini,但您可以使用.htaccess文件,您可以将其放在.htaccess:

php_value auto_prepend_file "/path/to/your/file.php"