Dynamically Load Css

I am working on my local machine. I have folders containing different sites within my document root.

My file structure looks like this:


Inside my functions.php file I declare a global variable as follows:

$docRoot = $_SERVER['DOCUMENT_ROOT'] . '/website';

At the top of each index.php file I include functions.php in addition I also include header.php and footer.php.

Now in my header.php file I am trying to link to my css file as follows:

<link rel="stylesheet" type="text/css" href="<?php echo $docRoot . "/css/layout.css"; ?> />

which results in the following: <link rel=“stylesheet” type=“text/css” href=“C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/website/css/layout.css” />

However, the styles don’t take effect. The styles would undoubtedly load properly on a live server, however, I work on my local development server and then move to the live server. I need a solution that will allow for development and easy migration.

Additionally, if I load the styles as follows:

  <style type="text/css">
  	<?php include_once $docRoot . "/css/layout.css"; ?>

They work properly on the mainpage index.php file only because of the relative paths of the images within the css file.

Anyone have a better solution for this?


We just use a typical define in our configuration, we don’t use the $_SERVER values.
That way, I can use multiple sites on localhost.

define('__BASE_URI__', '/website/');

Then anytime I need it, I just use


This is how it would look for your case:

<link rel="stylesheet" type="text/css" href="<?php echo __BASE_URI__."css/layout.css"; ?> />

Thanks for the quick reply, however, neither seem to work the way I want.

$docRoot = realpath($_SERVER['DOCUMENT_ROOT']) . '/website'; 

Results in the same full path starting at C.

$docRoot = basename($_SERVER['DOCUMENT_ROOT']) . '/website'; 

Results in ‘htdocs/website’

See if one of these 2 work

$docRoot = realpath($_SERVER['DOCUMENT_ROOT']) . '/website';
$docRoot = basename($_SERVER['DOCUMENT_ROOT']) . '/website';