【问题标题】:Absolute Address for CSSCSS 的绝对地址
【发布时间】:2023-03-25 12:45:01
【问题描述】:

我的 PHP 本地主机上有以下文件结构:

    • css/
      • main.css
    • 管理员/
      • admin.php
    • index.php
    • header.php

admin.php 和 index.php 都需要 header.php,其中 main.css 包含如下:

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

但是,css 文件未正确包含。当我在 Mac 上查看 Chrome 中的源代码时,浏览器已经把它变成了

http://localhost/Users/ljhljh235/Documents/web/hetd/css/main.css

其中http://localhost 不打算在这里。任何人都可以帮助我如何为 css 文件构建正确的绝对路径吗?

附:我在 admin.php 和 index.php 中包含 header.php 的方式是

require_once ($_SERVER['DOCUMENT_ROOT'].'/header.php');

我正在使用 MAMP 2.1.1。

感谢您的帮助。

【问题讨论】:

    标签: php css require-once


    【解决方案1】:

    变量 $_SERVER['DOCUMENT_ROOT'] 用于服务器端路径,仅使用 /css/main.css,它将与域相关,即:http://domain.com/css/main.css

    【讨论】:

    • 不客气,记住不以 / 开头的路径是相对于当前路径的路径,带 1 是相对于根路径的路径。
    • 如果你真的需要完整的路径,你也可以试试 http:///css/main.css
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    相关资源
    最近更新 更多