Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2006
    Location
    Mid-Missouri, USA
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Mar 2006
    Location
    Mid-Missouri, USA
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    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. 
    Last edited by CFMaBiSmAd; 05-02-2008 at 09:41 PM.
    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.

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Its a bit of an overkill in my oppinion. But if it makes your life easier...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •