Locating the root site

I need help with concepts and terminology regarding URLs and root URLs.

Is it possible to determine the root of the site or is it an arbitrary idea, and only the actual root of the server can be installed?

Let's say I'm writing a PHP plugin that will be used by different websites in different places, but must determine what the base directory of the website is. Using PHP, I can always determine DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and the absolute path to the server directory (or virtual server). But I do not know if the site itself is installed in the root directory or in a subdirectory. If the website was in a subdirectory, I would need to explicitly specify the subpath variable. Right?

+6
source share
4 answers

Answer to question 1: Yes, you need a variable that explicitly sets the root path of the website. This can be done using the 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 using:

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

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

+13
source

Will the $ url and $ dir pointer point 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; 

Conclusion:

 sandbox.phpcode.eu/hello /data/sandbox//hello 
+5
source

You do not need to ask the user for any information.

This snippet will let your code know if it works 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']; } ?> 
0
source

I had the same problem. I would like to link to links and other files from the root directory of my site structure.

I tried the following, but it would never work as 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 to simply use the following:

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

Source: https://habr.com/ru/post/897490/


All Articles