Absolute Includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Absolute Includes

Post by jkies00 »

Hi All,

PHP newb here. I'm creating a website and I'd like to include my css stylesheets on each page using a php include. My site has many page at diferent folder depths, which can be a problem. After some research, I learned that I can include the php file from any folder depth by using the following syntax:

Code: Select all

<?php include($_SERVER["DOCUMENT_ROOT"].'/_includes/components/stylesheets.php'); ?>
This enabled me to get to the include I want from all of my pages, BUT the paths within the includes would be broken. Here's my stylesheet include:

Code: Select all

<link rel="stylesheet" type="text/css" href="_includes/css/main.css" />
<link rel="stylesheet" type="text/css" href="_includes/css/navigation.css" />
Obviously, if this file is included anywhere but in the root, the paths to the css files will be wrong. So I tried this:

Code: Select all

<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER["DOCUMENT_ROOT"]  ?>/_includes/css/main.css" />
<link rel="stylesheet" type="text/css" href="_includes/css/navigation.css" />
...and that's where I'm stuck. When I refresh the page after having made that change, the styles aren't applied to the page. BUT, when I look at the source code, the href path points to the proper place in my local file structure. (i'm developing this locally)

So what am I missing? While putting $_SERVER["DOCUMENT_ROOT"] into my path to my css file might work, it seems like a really inefficient way to handle includes. Is there a better way?

Thanks in advance,
jeff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

paths in HTML are site relative, not file system relative. So just make the href's /_includes/css/
Post Reply