【问题标题】:Functions stop working when file included with root path (leading slash)当文件包含在根路径中时功能停止工作(前导斜杠)
【发布时间】:2013-06-18 20:15:16
【问题描述】:

我的根目录中的 PHP 文件包括 header.php。 Header.php 包括 functions.php。我在子目录中添加新页面,因此我在 header.php 中的所有链接中添加了前导斜杠:CSS、菜单项和随后的 INCLUDE 到 functions.php。 CSS 和菜单项在此页面的子目录中工作正常,但功能不起作用。函数中没有似乎需要前导斜杠的链接。

include 和前导斜杠的组合是否需要修改函数?

从根目录中的页面:

include('header.php');

从子目录中的页面:

include('/header.php');

来自 header.php:

include('/functions.php');

以及不再起作用的函数(从根目录或子目录的页面调用):

function show_date($array_name){
if (date("Y F j",strtotime($array_name["exhibit_open"])) == date("Y F j",strtotime($array_name["exhibit_close"]))){
    echo date("F j, Y",strtotime($array_name["exhibit_open"]));
}
elseif (date("Y",strtotime($array_name["exhibit_open"])) != date("Y",strtotime($array_name["exhibit_close"]))) {
    $first_date_format = "F j, Y";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} elseif (date("F",strtotime($array_name["exhibit_open"])) != date("F",strtotime($array_name["exhibit_close"]))){
    $first_date_format = "F j";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} else {
    $first_date_format = "j";
    echo date("F j",strtotime($array_name["exhibit_open"])). " - ". date($first_date_format,strtotime($array_name["exhibit_close"])). ", ". date("Y",strtotime($array_name["exhibit_close"]));
}

}

【问题讨论】:

  • 当前文件夹使用include('./functions.php');

标签: php function include


【解决方案1】:

标准路径 101:

/path/somefile - 前导 / 将此路径结构锚定在文件系统的根目录,例如相当于C:\path\somefile

path/somefile - 没有前导 /。操作系统将使用程序“当前工作”目录作为路径的基础,因此如果您在/home/foo 中的shell 中,那么somefile 将在/home/foo/path/somefile 中搜索。

../somefile.. 指的是当前工作目录的 PARENT 目录,所以如果你在/home/foo,那么../somefile 将被搜索为/home/somefile

请注意,您可以使用无意义的路径,例如

/../../../../somefile。这是可以接受的,但毫无意义,因为你们都将路径锚定在文件系统的根目录,然后尝试超越根目录,这是不可能的。此路径相当于/somefile

【讨论】:

    【解决方案2】:

    请注意,如果您要请求的 php 页面本身也请求其他页面,则使用 require_once 而不是 include 可能会有所帮助。这样一来,所包含的页面都不会重复,您不必担心不小心包含了不止一次。

    话虽如此...当您在根目录中请求页面时,它会在根目录中请求 header.php,然后在根目录中请求functions.php。但是,如果您从子目录请求,../header.php 将引用根目录中的 header.php,但整个文件将被包含在内,然后子目录中的 php 页面最终会尝试包含 /functions.php。它需要请求../functions.php,但这会导致根目录中的所有内容都停止工作。

    我建议在 header.php 中按照$root = $_SERVER['DOCUMENT_ROOT']; 的行设置一个变量然后,header.php 中的所有包含应该像include($root."/functions.php");

    $_SERVER['DOCUMENT_ROOT'] 将为您提供指向根目录的客观 URL,这将使您能够确保无论您从何处请求 header.php,您都引用了正确的位置。

    【讨论】:

    • 我想到了这一点,但是我的一些链接是简单的 HTML(例如样式表),所以我认为只添加一个前导斜杠会产生相同的效果。 (我的意思是说 /header.php 而不是 ../header.php,并且已经编辑了原始帖子)。为什么 $_SERVER['DOCUMENT_ROOT'] 更可取?
    • 假设您在一个目录中拥有 index.php、header.php 和 functions.php。然后你在一个子目录中有 otherpage.php。 index.php 调用include('header.php');。 header.php 中的所有代码突然变成了 index.php 代码的一部分。那么 index.php(不是 header.php)调用include('functions.php');。这很好,因为functions.php 在同一个目录中。但是如果 otherpage.php 调用 include('../header.php');,那么 header.php 中的代码会被吸收到 otherpage.php 中,现在 otherpage.php 尝试调用 include('functions.php');,它找不到。
    • $_SERVER['DOCUMENT_ROOT'] 将使得在 header.php 中你可以调用include($_SERVER['DOCUMENT_ROOT']."/functions.php");,这样无论你试图从哪里包含 header.php,所有 header.php 的包含仍然会工作。
    • 谢谢。因此 include('/functions.php') 不会将前导斜杠解释为对 root 的引用。我会使用你建议的代码。
    【解决方案3】:

    IncludeRequire 确实将代码拉入执行文件,因此需要注意的一件事是子目录中的文件是从工作目录运行的。

    例子:

             |-templates-|-header.php
    Docroot--|
             |-inc-|-functions.php
             |
             |-index.php
    

    索引.php

    <?php
    include 'template/header.php';
    ...
    ?>
    

    模板/header.php

    <?php
    include 'inc/functions.php';
    ...
    ?>
    

    因为由于包含,header.php 代码正在从 docroot 执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2017-09-29
      • 2021-03-08
      • 2023-04-06
      • 2020-11-11
      • 2018-03-30
      • 2016-12-12
      相关资源
      最近更新 更多