确定网站的“根”地点

发布于 2024-12-05 04:09:31 字数 268 浏览 5 评论 0原文

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

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

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

I need some help with concepts and terminology regarding website 'root' urls and directories.

Is it possible to determine a website's root, or is that an arbitrary idea, and only the actual server's root can be established?

Let's say I'm writing a PHP plugin that that will be used by different websites in different locations, but needs to determine what the website's base directory is. Using PHP, I will always be able to determine the DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and absolute directory path of the server (or virtual server). But I can't know if the website itself is 'installed' at the root directory or in a sub directory. If the website was in a subdirectory, I would need the user to explicitly set an "sub-path" variable. Correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不吻晚风 2024-12-12 04:09:31

问题 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

Answer to question 1: Yes you need a variable which explicitly sets the root path of the website. It can be done with an htaccess file at the root of each website containing the following line :

SetEnv APP_ROOT_PATH /path/to/app

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

And you can access it anywhere in your php script by using :

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

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

猫烠⑼条掵仅有一顆心 2024-12-12 04:09:31

$url 和 $dir 总是指向同一个地方吗?

<?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

Will $url and $dir always be pointing to the same place?

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;

Output:

sandbox.phpcode.eu/hello
/data/sandbox//hello
╄→承喏 2024-12-12 04:09:31

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

此代码片段将让您的代码知道它是否在根目录中运行:

<?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'];
        }
?>

You shouldn't need to ask the user to provide any info.

This snippet will let your code know whether it is running in the root or not:

<?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'];
        }
?>
稍尽春風 2024-12-12 04:09:31

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

我尝试了以下方法,但它永远不会按照我想要的方式工作:

$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>";

I have just had the same problem. I wanted to reference links and other files from the root directory of my website structure.

I tried the following but it would never work how I wanted it:

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

But the obvious solution was just to use the following:

echo "<a href="../index.php">Link</a>";
echo "<a href="../admin/index.php">Link</a>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文