PHP相对URL不工作


PHP Relative URL not working

我一直在尝试在PHP中设置一个模板头,将在我所有的页面上使用。然而,当我尝试使用根相对路径时,我得到了几个错误。我看过其他几个问题,并没有能够通过他们解决我的问题(通常是因为我无法掌握他们的目录设置;这就是我的图表)。我的安装是一个运行LEMP堆栈的Ubuntu 14.04服务器。在我的default.conf文件中,我设置了我的文档根目录,如下所示:

root /usr/share/nginx/html;
index index.php index.html index.htm;

目录结构:

...
 |
 | 
 +--ETC
 |   |
 |   +--NGINX
 |   |   |
 |   |   +--sites-available
 |   |       |
 |   |       +--default.conf   
 |   |   
 |   +--PHP5
 |       |
 |       +--FPM
 |           |
 |           +--php.ini
 |
 +--HOME
 |   |
 |   +--ADMIN
 |       |
 |       +--database.php
 |
 +--USR
     |
     +--SHARE
         |
         +--NGINX
             |
             +--[HTML]
                 |
                 +--INCLUDES
                 |   |
                 |   +--header.php
                 |   |
                 |   +--footer.php
                 |
                 +--PAGES
                 |   |
                 |   +--contact_us.php
                 |
                 +--index.php
                 +--database.txt

index . php

<?php require "includes/header.php" ?>
...
<? php require "includes/footer.php" ?>

这些链接没有根相对路径…


header。php

<!DOCTYPE html>
<head> ... </head>
<body>
    <?php require ("../../../../home/admin/database.php") ?>
    <header>
        ...
        <a href="/index.php">Home</a>
        ...
        <a href="/pages/contact_us.php">Contact Us</a>
        ...
    </header>
</body>

…但这些必须。如果它们被切换,两者都会崩溃。此外,我不知道如何做上面的require语句更好,因为它在文档根之上,我不确定它可以从哪里调用。是否有一种方法可以访问"服务器"根,而不仅仅是"文档"根?


contact_us.php

<?php require "/includes/header.php"; ?>
....
<?php require "/includes/footer.php"; ?>

…这页也一样。


如您所见,我使用了几种不同类型的url,其中大多数是根相对路径。如果我试图在我的index.php文件中使用根相对路径,但是,我得到一个空白屏幕,因为它无法找到header.phpfooter.php文件(这是必需的)。

我在其他页面也没有更好。在我的header.php文件中,您会注意到我有一个contact_us.php页面的URL作为我的标题的一部分。然而,当我在索引页上开始并单击Contact Us页面的链接时,我得到了这个错误,即使我已经明确拼写了正确的相对路径(记住,"html"文件夹是我的根目录)。

Warning: require(/includes/header.php): failed to open stream: No such file or directory in /usr/share/nginx/html/pages/contact_us.php on line 4`
Fatal error: require(): Failed opening required '/includes/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/nginx/html/pages/contact_us.php on line 4`

基本上,我想要的是确保我所有的url从同一个地方开始。我知道使用../有时会更快,但由于我的许多页面都有include语句,因此这种方式可能会很快破坏url。是否有一种方法可以从根目录开始,在任何页面上工作,无论它在网站上的位置,或者它从哪里被调用?


编辑:我刚刚尝试了卢平建议的"<?php require $_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"; ?>"的想法。虽然这确实得到了一点父亲(背景颜色加载),然后它崩溃,并说这个错误消息:

Warning: require(../../../../home/admin/database.php): failed to open stream: No such file or directory in /usr/share/nginx/html/includes/header.php on line 30
Fatal error: require(): Failed opening required '../../../../home/admin/database.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/nginx/html/includes/header.php on line 30

注:为了清楚起见,我的文档根目录是/usr/share/nginx/html

问题是,当您使用根路径的require函数时,PHP不考虑您的项目或web根作为根目录,而是引用您的系统的根目录!

当请求require('/includes/header.php')时,您实际上是在请求:

/includes/header.php

而不是你想要的:

/usr/share/ngix/html/includes/header.php

所以-要解决这个问题,我们需要某种常量,总是指向你的服务器网站根-就像你在nginx中定义的那样。这就是$_SERVER['DOCUMENT_ROOT']派上用场的地方

http://php.net/manual/en/reserved.variables.server.php

只要PHP/nginx配置正确,这应该指向/usr/share/nginx/html

将您的要求更改为:

<?php require($_SERVER['DOCUMENT_ROOT'] . 'includes/header.php'); ?>

应该可以。

小心不要混淆url和系统路径- PHP根据系统路径思考,而你的HTML将根据url工作。

希望有帮助!

如果你的服务器文件的根目录是/usr/share/nginx/html,你可以使用$_SERVER['DOCUMENT_ROOT']

require $_SERVER['DOCUMENT_ROOT'].'/includes/header.php';

对于位于服务器根目录之外的数据库文件-将配置文件置于文档根目录之外是一种良好的做法,但是如果它位于根目录之外的一个级别或多个级别,则没有区别。为了访问数据库文件,我认为你有两个选项:

  1. 添加一个常量,其中包含数据库文件的真实位置——这将是最简单的,但会影响站点在不同服务器之间的迁移,并且当您在不同的系统上运行站点时,您需要记住更改它。

  2. 定位数据库文件更接近文档根-让我们说一个级别,然后操作你从$_SERVER['DOCUMEN_ROOT']得到的字符串。

下面的示例演示了当数据库文件比根

高一级时如何获取位置
$x = rtrim($_SERVER['DOCUMENT_ROOT'], ''/');
$x = substr($x, 0, (strrpos($x, '/')+1));
$x = $x.'/folder/database.php';