PHP includes

software development

#1

When I first began to create my web page, I found that PHP allowed me to have standard sections of HTML code that I could “paste” into all of my pages. All of my include files are stored in an “includes” folder within the root level of my site. I wanted the header of each of my pages to be identical, and originally, it looked like this:

<?php define( "INCLUDE_ROOT", "http://www.chivalrysoftware.com/includes/" ); define( "CURRENT_ITEM", "About" ); define( "CURRENT_ITEM_CLASS", " class=\"currentlistitem\"" ); define( "CURRENT_ITEM_STYLE", " style=\"background-color: #C4C769\"" ); // For everything that appears before the tag include INCLUDE_ROOT . "pre_head.include"; // The tag is identical except for part of the tag. The difference in the // title is what appears between this include and the following one. include INCLUDE_ROOT . "head_1.include"; echo " About Chivalry Software"; include INCLUDE_ROOT . "head_2.include"; ?>

Well, when Dreamhost disabled the ability to include external files, my site stopped working because I was referencing a full URL in my include statements.

I had used the full URL because I couldn’t figure out how to get each of these code portions in each of the files (with the exception of the echo command) to be identical without using the full URL. Now that external includes have been disabled, I have two different versions of the above code. The first is for files that are in the root level:

define( “INCLUDE_ROOT”, “~/chivalrysoftware.com/includes/” );

This version was an attempt to use the same line of code in all files. I’m assuming that if that weren’t a requirement, I could have used the line:

define( “INCLUDE_ROOT”, “/includes/” );

The second version is for files that are one folder deep:

define( “INCLUDE_ROOT”, “…/includes/” );

So, my question becomes, why won’t the first version work in files that are one folder deep? Actually, I care less about that than how to use the exact same define for all files, so if anyone has a solution to that, I would be very grateful if it were shared. :slight_smile:

Thanks,
Chuck


#2

You will be able to find the absolute path (relative to root) with this environment variable:$_SERVER['DOCUMENT_ROOT']Therefore, you should be able to change your initial declaration to this:<?php define("INCLUDE_ROOT", $_SERVER['DOCUMENT_ROOT']."/includes/"); ?>---------------
Simon Jessey
Keystone Websites | si-blog


#3

Thanks very much for the quick reply. I replaced the first define in all of my files with the line you suggested, and it worked great (thank goodness for BBEdit multi-file replace :).

Chuck