Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2008, 12:56 PM   PM User | #1
panther21001
New to the CF scene

 
Join Date: Mar 2006
Location: Mid-Missouri, USA
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
panther21001 is an unknown quantity at this point
inlude() addon feature - finds the include file no matter where you are

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
panther21001 is offline   Reply With Quote
Old 05-02-2008, 02:22 PM   PM User | #2
RMcLeod
New Coder

 
Join Date: Mar 2008
Location: Somerset, England
Posts: 93
Thanks: 0
Thanked 10 Times in 10 Posts
RMcLeod is an unknown quantity at this point
Why not set your inlude path in php.ini to your includes folder. That way any files in this folder can be included from anywhere within your site structure.
RMcLeod is offline   Reply With Quote
Old 05-02-2008, 06:17 PM   PM User | #3
panther21001
New to the CF scene

 
Join Date: Mar 2006
Location: Mid-Missouri, USA
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
panther21001 is an unknown quantity at this point
Quote:
Originally Posted by RMcLeod View Post
Why not set your inlude path in php.ini to your includes folder. That way any files in this folder can be included from anywhere within your site structure.
Well their are some reasons not to do that.

1: I don't have control of the php.ini file with my hosting plan, and most people don't.

2: This class can be used with any website or project on your server (if you have multiple domains hosted on the same account)

And their may be some other drawbacks as well. Why edit the php ini? That may be going a little too far and much more of a hassle later on. But thanks for that option, it may suit certain people's needs.

-Philip
panther21001 is offline   Reply With Quote
Old 05-02-2008, 08:17 PM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
This could be a security risk if you have a file of the same name in a parent directory(for example multiple installs of the same script). Also, this shouldn't need a class for just one function.
Inigoesdr is offline   Reply With Quote
Old 05-02-2008, 08:30 PM   PM User | #5
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
A) You can set the include path in your script using either the set_include_path() statement or an ini_set() statement.

B) That's a lot of time consuming code and resources to just replace $_SERVER['DOCUMENT_ROOT'] in front of your path/file.

PHP Code:
include($_SERVER['DOCUMENT_ROOT'] . '/' FOOTER); // note, document root does not include the trailing slash and your defined constant does not include a leading slash, so my example here includes a slash as part of the code. It would be better to include the slash as part of the defined constant. 
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

Last edited by CFMaBiSmAd; 05-02-2008 at 08:41 PM..
CFMaBiSmAd is offline   Reply With Quote
Old 05-04-2008, 04:16 PM   PM User | #6
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
Its a bit of an overkill in my oppinion. But if it makes your life easier...
idalatob is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:37 AM.


Advertisement
Log in to turn off these ads.