PDA

View Full Version : how do I include a document from any file on my site?



pythonscript
12 Jun 2011, 08:59 PM
I'm trying to use templates on my site that I include with the include_once() php statement. Unfortunately, I cannot figure out how to include files *globally*. This is my directory structure:



home
user
public_html
css
site.css
files
a_file.php
templates
main.php
index.php


I'm in a_file.php, and I'm trying to use the site.css style sheet (which doesn't use an include statement, bad example) as well as main.php. However, if I use include_once(/templates/main.php), the file can't be found. If I use include_once(/home/user/public_html/templates/main.php), the file can't be found. If I use include_once(../templates/main.php), it works, but here's the main question.

What would I use to reference that style sheet and the php file throughout the entire site? Is there a single way to refer to it (something like /home/user/public_html/templates/main.php) that's the same no matter where I put it in public_html? It defeats the *entire* purpose of templates if I can't store them in a subfolder or have to add a new one every time I use it for a page in a different folder.

Thank you for the help!

EDIT: Someone referred me to the $_SERVER['DOCUMENT_ROOT'] value in PHP, but I'm still having major trouble getting my site to work. Here's the code in my header.php file, which is included on each page (I removed all meta tags to make it simpler).



<?php $doc_root = $_SERVER['DOCUMENT_ROOT']; ?>
<head>
<title>example.com</title>

<link href="<?php echo $doc_root."/css/site.css"; ?>" rel="stylesheet" type="text/css"/>
</head>


In index.php, I have this code to include it:



<?php $doc_root = $_SERVER['DOCUMENT_ROOT']; ?>
<?php include_once($doc_root."/templates/header.php"); ?>


When I view the source code of index.php in the browser, the header is 100% there, and the <link/> line that points to the style sheet points to /home/user/public_html/css/site.css, which is correct. Unfortunately, the page isn't rendered using the style sheet, since the formatting is completely blank.

Any ideas? I know I'm declaring $doc_root twice, but it doesn't seem to work if I don't include it in the header.php file as well.