6

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?

Yarin
  • 144,097
  • 139
  • 361
  • 489

4 Answers4

14

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

Imad Moqaddem
  • 1,323
  • 8
  • 10
7

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
Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
genesis
  • 48,512
  • 18
  • 91
  • 118
  • Don't understand the example- that's just showing different paths, but not indicating its resolving to diff locations – Yarin Sep 16 '11 at 19:21
  • Moved this portion of question to here: http://stackoverflow.com/questions/7449367/help-understanding-path-resolutions-web-urls-vs-directories – Yarin Sep 16 '11 at 19:23
  • @Yarin: http://stackoverflow.com/questions/7449367/do-serverserver-name-and-serverdocument-root-always-resolve-to/7449464#7449464 – genesis Sep 16 '11 at 19:31
0

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'];
        }
?>
Dayo
  • 11,009
  • 5
  • 44
  • 65
-1

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>";
  • 1
    In what way is it obvious? Your solution would only work in it's current form if the current folder is one step down from the root. As such, you need to know the root path / url in order to make the relevant number of steps back eg how many times you need to use '../' – Phill Healey Mar 28 '16 at 19:34