note 31804 deleted from function.include by sniper
| From: | sniper@php.net | Date: | Tue, 04 Nov 2003 00:41:45 +0000 |
| Subject: | note 31804 deleted from function.include by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59787@lists.php.net to get a copy of this message | ||
Note Submitter: gmgiles@pacbell.net
----
How to minimize use of absolute filepaths in include() statements across your website, and make your
coding life simpler:
Using absolute paths to include() files such as custom function libraries is problematic because it
relies on a fixed directory structure that must never change - if you reorganize your code or change
the name of an include file, you'll have to go back through your code and update every effected
include(). That's a huge waste of time and a big hassle. Here's a way to get around that.
First, in the root directory of your website, create a file called includes.php. As the name
implies, this file will make use of define() to centralize all the absolute filenames that would
otherwise be strung throughout your code. The benefit is that if you restructure your code (i.e.
rename or move an include file), you only need to edit this one file to make the change propogate
site-wide. Here's a simplified example:
[includes.php]
/*** begin includes.php ***/
// defines
define ("ROOT", "/www/path_to/your_website_dir/"); // filesystem path to root
directory
define ("URL_ROOT", "http://www.mydomain.com/"); // absolute URL to
website
// my custom function libraries
define("DATABASE_H", ROOT."db_control.php"); // database control functions
define("MESSAGE_H", ROOT."message.php"); // system/error message functions
define("SESSION_H", ROOT."session.php"); // sets session information to be
used in other functions
define("SYSTEM_CONFIG_H", ROOT."system_config.php"); // system configuration
functions
define("USER_H", ROOT."user.php"); // general user information functions
// commonly used page definitions
define ("PAGE_ADMIN", URL_ROOT."admin/admin.php"); // admin page
define ("PAGE_LOGIN", URL_ROOT."login.php"); // login page
define ("PAGE_USER", URL_ROOT."myaccount/manage.php"); // user account home
page
define ("PAGE_LOGOUT", URL_ROOT."logout.php"); // logout page
/*** end includes.php ***/
In the above code, I use define() to create new constants called ROOT, DATABASE_H, MESSAGE_H,
SESSION_H and so on. Those constants are what I will use throughout my website when I need to
include() other files - see example.php below. You'll note that some constants are for my
custom function libraries, and some are for commonly used pages, such as the login and logout pages
(PAGE_LOGIN, PAGE_LOGOUT, etc.). Note that some constants reference files in different
subdirectories.
Also note that some defines rely on ROOT and others on URL_ROOT; that's because those that rely
on ROOT are all custom libraries that the filesystem needs to find, and those that use URL_ROOT are
page definitions that rely on a fully qualified domain name that the webserver needs to find.
The following example can be located anywhere within your website's directory structure, and
because we've defined the paths elsewhere, you no longer have to bother with absolute paths in
your code - just use the constants you defined in includes.php:
[example.php]
/*** begin example.php ***/
<?
// Include includes.php so all our new defines() can be found
include $_SERVER['DOCUMENT_ROOT']."/includes.php"; // include paths
// Include the custom library functions we need to use.
// In some instances, using require_once() might be better. YMMV.
include DATABASE_H; // database control functions
include SESSION_H; // session functions
include USER_H; // general user information functions
// go to a specific, commonly used page in the site
header("Location: ". PAGE_USER);
/*** end example.php ***/
This method does require that you NEVER rename or move includes.php, but it is very transportable
and a big time saver. Hope this helps simplify your coding life a bit! :)
?>