开发者

Determining a website's 'root' location

开发者 https://www.devze.com 2023-04-05 15:47 出处:网络
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

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 estab开发者_开发问答lished?

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?


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消