html - PHP getting document path relative to site root

227

I'm trying to link a separate php document in the header files that contains all of the CSS link information so that if I want to change the site's design I only have to change the css path in one location (in particular for various color schemes. As I add more schemes I can just put them in a switch statement in this one file instead of going through every single page.

I'm trying to write the code so that it will work regardless of which server it's running on (my local test server or the remote site server) without changing any path information.

From what I was reading it seems like$_SERVER['DOCUMENT_ROOT'] is the best way to find the path to the site's base folder so that I can find the server folder/css files regardless of where the page file is located.

here is an example of how I have it set up:

and the CSS php file is like this:

   <?php
echo "<link rel='stylesheet' type='text/css' href='styles/default.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/basicblue.css'/>";  
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/forms.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/loginform.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/newscontent.css'/>";
?>

I'm positive DOCUMENT_ROOT is set to the correct location, but my styles aren't showing up. am I missing something? Is there a more reliable way to set this up?

12

Answer

Solution:

Echoing what Mike said above, in my experience,$_SERVER['DOCUMENT_ROOT'] is only the best option for finding files on the server. If you need php to inlcude or require something, find the server side path withDOCUMENT_ROOT.

However, css files are client side. They're included from the relative website path. If you were to instead just do

<link rel='stylesheet' type='text/css' href='/styles/newscontent.css'/>

The opening/ in the href tells the browser to always retrieve it from root of your domain:http://yourdomain.com/styles/newscontent.css.

196

Answer

Solution:

You have to use$_SERVER["DOCUMENT_URI"] instead of$_SERVER["DOCUMENT_ROOT"] , like this:

echo "<link rel='stylesheet' type='text/css' href='" . dirname($_SERVER['DOCUMENT_URI']) . "/styles/basicblue.css'/>";
525

Answer

Solution:

I ended substracting the web root path to get the relative one

str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER["SCRIPT_FILENAME"]);

So instead of/home/u/username/example.com/public_html/webfolder/index.php you will get/webfolder/index.php

edit: in constructed case your filename somehow can repeat document root, so the code above will not work correctly. This is more bulletproof (count symbols and remove them)

substr($_SERVER["SCRIPT_FILENAME"],strlen($_SERVER['DOCUMENT_ROOT']));
653

Answer

Solution:

My case is including a JS script, which is located in the same folder as the PHP script.

I used the following to get the path from the site root to the script folder:

<script src="<?php echo str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__); ?>/script.js"></script>

This works fine.

People are also looking for solutions to the problem: java - Android VMD MySQL insert and showing dialog

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.