【问题标题】:How can I create a absolute url for my site using php?如何使用 php 为我的网站创建绝对 url?
【发布时间】:2012-02-29 15:07:18
【问题描述】:

您好,我正在尝试使用 php 创建一个网站,其结构如下所示:

-index.php
-header.php
-footer.php
-style.css
-images\{images are in this folder}
-\web\index.php
-\contact\index.php

问题是我在所有 index.php 文件上使用 php 的包含函数来使用 header.php 和 footer.php 文件,但问题是只有根文件夹中的 index.php 显示样式,但是当我打开 localhost/web/index.php 时,由于找不到 css 和图像,页面看起来很糟糕。

我该如何解决这个问题。

请帮忙

【问题讨论】:

  • 确保使用绝对 URL 来包含 css、图像和 js。
  • 你能告诉我如何使用绝对路径
  • 使用静态文件的完整路径例如:而不是images/abc.gif 使用/images/abc.gif

标签: php web


【解决方案1】:

添加网站名称,如

$site_base_url = "http://localhost/web";

当你使用 php 时,将它添加到 url 前面 喜欢

echo '<link type="text/css" href="'.$site_base_url.'/style.css" rel="stylesheet" charset="utf-8">'

或者喜欢

echo '<img src="'.$site_base_url.'/images/file_name.jpg" />'

当你使用函数时,像这样使用global

function find_user(){
global $site_base_url;
echo "no users found";
echo '<a href="$site_base_url">return home</a> 
}

【讨论】:

    【解决方案2】:

    你可以require_once($_SERVER['DOCUMENT_ROOT'].'\header.php');

    【讨论】:

      【解决方案3】:

      试试这个结构:

      • index.php

      • styles/style.css

      • templates/header.php

      • templates/footer.php

      • images/{Images go here}

      我强烈建议您阅读this 教程。这对初学者来说真的很有帮助,也很容易。

      【讨论】:

      【解决方案4】:

      在你的 style.css 中你可以使用

      background-image:url(/images/mycoolpic.png);
      

      【讨论】:

        【解决方案5】:

        非常简单。
        您必须从站点根目录开始每条路径,即/

        因此,将 style.css 寻址为 /style.css 将允许从站点的任何页面调用它。
        网站上的所有本地超链接也是如此 - 只需从 / 开始它们(当然后面是正确的路径)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-18
          • 2011-02-01
          • 2017-06-28
          • 1970-01-01
          • 2011-03-23
          • 1970-01-01
          • 2014-03-29
          • 1970-01-01
          相关资源
          最近更新 更多