【问题标题】:Determining a website's 'root' location确定网站的“根”位置
【发布时间】:2011-11-18 22:07:02
【问题描述】:

我需要有关网站“根”网址和目录的概念和术语方面的帮助。

是否可以确定网站的根目录,或者这是一个随意的想法,只能建立实际服务器的根目录?

假设我正在编写一个 PHP 插件,它将被不同位置的不同网站使用,但需要确定网站的基本目录是什么。使用 PHP,我将始终能够确定 DOCUMENT_ROOT 和 SERVER_NAME,即服务器(或虚拟服务器)的绝对 URL 和绝对目录路径。但我不知道网站本身是“安装”在根目录还是子目录中。如果网站位于子目录中,我需要用户明确设置“子路径”变量。对吗?

【问题讨论】:

标签: php path document-root base-url


【解决方案1】:

$url 和 $dir 会一直指向同一个地方吗?

Yes

<?php 
$some_relative_path = "hello"; 
$server_url = $_SERVER["SERVER_NAME"]; 
$doc_root = $_SERVER["DOCUMENT_ROOT"]; 


echo $url = $server_url.'/'. $some_relative_path."<br />"; 
echo $dir = $doc_root.'/'. $some_relative_path;

输出:

sandbox.phpcode.eu/hello
/data/sandbox//hello

【讨论】:

【解决方案2】:

问题 1 的答案:是的,您需要一个明确设置网站根路径的变量。可以使用每个网站根目录下的 htaccess 文件来完成,其中包含以下行:

SetEnv APP_ROOT_PATH /path/to/app

http://httpd.apache.org/docs/2.0/mod/mod_env.html

您可以在 php 脚本中的任何位置使用:

<?php $appRootPath = getenv('APP_ROOT_PATH'); ?>

http://php.net/manual/en/function.getenv.php

【讨论】:

    【解决方案3】:

    您不需要要求用户提供任何信息。

    这个 sn-p 会让你的代码知道它是否在根目录下运行:

    <?php
            // Load the absolute server path to the directory the script is running in
            $fileDir = dirname(__FILE__);
    
            // Make sure we end with a slash
            if (substr($fileDir, -1) != '/') {
                $fileDir .= '/';
            }
    
            // Load the absolute server path to the document root
            $docRoot = $_SERVER['DOCUMENT_ROOT'];
    
            // Make sure we end with a slash
            if (substr($docRoot, -1) != '/') {
                $docRoot .= '/';
            }
    
            // Remove docRoot string from fileDir string as subPath string
            $subPath = preg_replace('~' . $docRoot . '~i', '', $fileDir);
    
            // Add a slash to the beginning of subPath string
            $subPath = '/' . $subPath;          
    
            // Test subPath string to determine if we are in the web root or not
            if ($subPath == '/') {
                // if subPath = single slash, docRoot and fileDir strings were the same
                echo "We are running in the web foot folder of http://" . $_SERVER['SERVER_NAME'];
            } else {
                // Anyting else means the file is running in a subdirectory
                echo "We are running in the '" . $subPath . "' subdirectory of http://" . $_SERVER['SERVER_NAME'];
            }
    ?>
    

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题。我想从我的网站结构的根目录中引用链接和其他文件。

      我尝试了以下方法,但它永远无法达到我想要的效果:

      $root = $_SERVER['DOCUMENT_ROOT'];
      echo "<a href="' . $root . '/index.php">Link</a>";
      echo "<a href="' . $root . '/admin/index.php">Link</a>";
      

      但显而易见的解决方案就是使用以下内容:

      echo "<a href="../index.php">Link</a>";
      echo "<a href="../admin/index.php">Link</a>";
      

      【讨论】:

      • 在什么方面是明显的?如果当前文件夹比根目录低一级,您的解决方案只能以当前形式工作。因此,您需要知道根路径/ url 以便退回相关的步骤数,例如您需要使用多少次'../'
      猜你喜欢
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2013-07-14
      相关资源
      最近更新 更多