I began learning PHP and MySQL a month ago, and the thing I was irritated most about was losing control of my include files (I'm sure I'm not the only person).
I created a very simple class/function so that including files in your site will never be as big of a hassle.
Here is where you create and initialize your class object. (e.g: class_lib.inc.php)
PHP Code:
<?
class Includes
{
/**
* include_file - Assists the standard include function
* by automatically finding the correct path the specified
* include file is stored in.
*/
function include_file($incFile)
{
while (!file_exists($incFile) && strlen($incFile) < 100)
{
$incFile = "../" . $incFile;
}
if (!file_exists($incFile))
{
die("Include file " . $incFile . " could not be found.");
}
include ($incFile);
}
}
//Initialize Footer Object
$incFooter = new Includes();
?>
As an example, in your file (e.g: index.php) you would place this line of code:
PHP Code:
<? $incFooter->include_file(FOOTER); ?>
In my case, I defined the path to my footer include file in a constants.inc.php document:
PHP Code:
<?
//INCLUDE FILE PATHS
define("FOOTER", "required/include/footer.inc.php");
?>
How this works:
The $incFile variable holds the directory (from the 'www' root of your web directory), in my case
required/include/footer.inc.php which is where my footer include file is located. The while loop checks through your directory, no matter where the index.inc.php is located, and prepends (if necessary) $incFile with
"../" as many times as needed.
Please be aware I have only tested with PHP 5.
Let me know if anything can be changed or made better, as I am technically a 'newbie' coder.
I would like to thank everyone else's contribution to the forums, I have been on here a lot lately and learning very neat tips and tricks. Thanks!
-Philip