Document Root


Problem: How to access an absolute location (e.g., the styles folder) from any folder/subfolder in the site.  This allows for more portability of code.

Solution: Define a string variable containing the uri of the document root.  In PHP this is stored in the Global supervariable $_SERVER["SERVER_NAME"].  Note that it does not end with a final backslash, so any additional folders to be added must start with one.  See example below.

 

code:

$root = "http://".$_SERVER["SERVER_NAME]"];
echo "<link rel='stylesheet' href='$root/bugs/styles/_home.css' type='text/css'>";

E.g., in the case of the bugs website this will be equal to "http://www.sickingfamily.com", but while developing it locally it will be equal to "http://localhost"
Note that the prefix "http://" is also added to the server name.  This is needed in some cases (notably for script and style links) to make the reference unambiguous.

References such as style links and script links require a uri, not a file spec, which is why we don't use $_SERVER["DOCUMENT_ROOT"].  $_SERVER["DOCUMENT_ROOT"] returns the file system pathname of the root (E.g., "D:/htdocs/sickingfamily", rather than "localhost/sickingfamily".