【问题标题】:DOCUMENT_ROOT or SERVER_NAME for showing CSS in subfoldersDOCUMENT_ROOT 或 SERVER_NAME 用于在子文件夹中显示 CSS
【发布时间】:2013-12-06 15:46:35
【问题描述】:

我在本地主机中遇到了这个奇怪的问题。我在 htcdocs 中有一个名为 sdis 的目录。我已将所有使用 DOCUMENT_ROOT 的文件包含在根文件夹下的文件中。所有的 css、链接、图像等都显示得很好。现在,当我在 sdis 下添加一个子文件夹并尝试包含它时,我得到一个没有任何 css 的原始页面。这是根文件夹文件和子根文件夹文件的代码。

<?php include $_SERVER["DOCUMENT_ROOT"].'/sdis/includes/overall/header.php'; ?>        
Whatever Data
<?php include $_SERVER["DOCUMENT_ROOT"].'/sdis/includes/overall/footer.php'; ?>

在玩了很多之后,我意识到,链接href部分是这样的:

href="bootstrap/css/bootstrap.css"

现在,当然不是绝对链接。因此,包含 DOCUMENT_ROOT 仅适用于根文件夹文件。所以我尝试使用这两种方法:

<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['SERVER_NAME'] ?>/bootstrap/css/bootstrap.min.css">

<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/bootstrap/css/bootstrap.min.css">

而且它仍然没有显示子根文件夹中文件的任何 css 或图像。

谁能帮助我,包括 css 链接、图像等,以便所有 css 和图像在根文件夹、子文件夹等中显示得太正确?

【问题讨论】:

  • 你能给我们举一个你的目录结构的例子吗?您是否只有一个包含所有内容的 css 目录?然后使用“/css/style.css”,因为“/css”就像“localhost/css”。

标签: php include href


【解决方案1】:

HTML 不知道生成它的 PHP 脚本。

最好的解决办法是设置虚拟服务器(Linux上/etc/hosts,Windows上%windir%/system32/drivers/hosts,可能需要重启)

127.0.0.1       localhost
127.0.0.1       vm1.localhost

然后在 httpd.conf 中

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/htcdocs"
</VirtualHost>

<VirtualHost *:80>
    ServerName vm1.localhost
    DocumentRoot "/htcdocs/yourfolder"
</VirtualHost>

修改后(重启你的服务器你可以通过http://vm1.localhost/访问你的页面,DOCUMENT_ROOT设置为yourfolder

注意:设置DOCUMENT_ROOT后,我推荐使用

href="/bootstrap/css/bootstrap.min.css"

而不是

href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/bootstrap/css/bootstrap.min.css"

使用 PHP 系统变量创建 HTML 路径可能会在您使用不同设置迁移到主机时强制您重写代码,而纯 HTML 保持独立。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-15
    • 2017-10-08
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2021-04-29
    • 2018-04-11
    相关资源
    最近更新 更多