12

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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<!--Meta Information-->

<!--CSS Info-->
<?php 
    require_once("styles/cssstyle.php");
?>

<title></title>
</head>
<body>

<!--pushes site down from top of screen -->
<div id="topmargin"></div> 

<!-- sets div for site content (puts in middle) -->
<div id="_body">          

    <div id="banner">                        
        <div class="logo"></div>      
        <div class="text"></div>
        <div class="bannerstrip"></div>
    </div>

    <!--portion for site navigation-->
    <div id="navigation">                               
        <ul class="navlinks">                            
            <li><a href="index.php">home</a></li>
        </ul>
    </div>

    <!--Holds all site usable/readable content-->
    <div id="leftwindow">                              

    </div>

    <div id="rightwindow">

    </div>

    <div id="rightwindow">

    </div>
</div>


</body>
</html>

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?

3
  • I'm positive it has something to do with finding the path because when I directly link to the styles in the index.php the styles work correctly Commented Dec 7, 2012 at 18:33
  • 3
    Have you tried looking at the generated source code? $_SERVER['DOCUMENT_ROOT'] returns the document root directory under which the current script is executing. This is a server-side path, and so it won't work client-side. Commented Dec 7, 2012 at 18:36
  • I have an apache server setup on my computer for testing. When I look at live code it looks like the path is correct. I browsed to the file to compare paths, and they are the same, which is what I don't fully understand. There is one difference: the slashes are in opposite direction, but that shouldn't matter for php, should it? Commented Dec 7, 2012 at 18:39

4 Answers 4

12

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 with DOCUMENT_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.

Sign up to request clarification or add additional context in comments.

3 Comments

for some reason when I add the / it still doesn't find it. It's only letting me find it relative to the document, not to the root folder
here's how it's showing up in live code: <link rel='stylesheet' type='text/css' href='/styles/default.css'/> but it's not linking properly. This means that cssstyle.php is being accessed as should be but the css links themselves are still busted
nevermind, figured it out. there was a problem with where the site root folder was pointing (it was pointing to the server root folder, not the site root folder)
5

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'/>";

1 Comment

Doesn't seem to work for me though (REQUEST_URI), not sure why its returning just "/" when the script is inside 3 sub folders.
2

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']));

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.