Re: Using include files ?

From: Mike Harvey (spam_at_spam.com)
Date: 10/29/04

  • Next message: Mike Harvey: "Re: Accessing function file on another domanin"
    Date: Fri, 29 Oct 2004 13:15:31 GMT
    
    

    If you are calling an include file which will not call any others you
    can just use:

    include("../header.php");
            or
    include("../../header.php");

    depending on how deep down the tree the file in question is.

    However, if the file you are calling is in turn calling other files
    and/or you are calling several files you can place this at the top of
    the page:

    $up_dir="../"; // adjust according to your needs.

    and then write all include() statments like this:

    include($up_dir."header.php");

    To take this one step further, I generally have all my include files
    in a "includes/" directory and images in a "images/" directory, etc. I
    also use a conficuration file that sets up global perameters for all
    pages that is kept in the "includes/" directory. So, at the top of
    every page I will have this:

    <?PHP
    $up_dir="../"; // depending on the files depth in the tree
                     // not used in files located in the root directory
    include_once($updir."includes/config.inc.php");

    Then in the config.inc.php file will be something like:

    <?PHP
    $path[inc]=$up_dir."includes/";
    $path[img]=$up_dir."images/";
            etc., etc.....

    Then, back to our original file, I write all include() statements like
    this:

    include("$path[inc]header.inc.php");
            or
    include($path[inc]."header.inc.php"); // if this looks clearer to you,
                                    // it doesn't matter which way.

    This all looks like a lot in the beginning but believe me, when you
    get to writing sites with dozens or even hundreds of files, it pays
    off in the long run to use this method, or similar.

    There are 2 main reasons for using this method:

    1) It is very common to include files from within included files. This
    is the only way you're going to pass the desired directory structure
    to the down line of included files.

    2) In functions (you should call your function file from within your
    config.inc.php file) all you have to do to pass on desired directory
    locations is declare the $path array as GLOBAL within the function.
    The function then has access to whatever directory is needed including
    the $up_dir information. Like this:

    function my_func($var1, $var2, var3, $var4){
     GLOBAL $path;
     $out="<IMG src=\"$path[img]$var1\" width=\"$var2\" height=\"$var3\"
    alt=\"$var4\" />"
     return $out;
    }

    Hope this helps.

    Mike H.
    http://vestudiohost.com
    http://ads-plus.com
    http://ibiz-tools.com
            etc., etc., etc....

    On Thu, 28 Oct 2004 14:20:08 +0100, "Tony Benham"
    <tonyb@kerrisway.freeserve.co.uk> wrote:

    >I'm learning PHP. One of the task I'm ytrying out is creating standard
    >scripts that contain the fixed elements of a webpage, and including these in
    >each webpage.
    >For instance if I have a standard header format, I want to put this into a
    >file say header.php. This will be included in each web page. The problem I
    >have is that my web pages are organized in a tree directory structure. So if
    >I have a header.php in the root directory of the web page, I want to include
    >it say in a file displaynews.php which is in a directory say called /news. I
    >have two problems, one is how to include the file header.php which is at
    >some different point in the directory structure. I could use include
    >'../header.php', and try to work out where it it is compared to where I'm
    >including it, or use the $document_root variable somehow, but I can't see
    >how to do that at the moment ?
    >The second problem is related. The header file refers to a couple of
    >images/relative links such as
    ><img src="logo3.jpg"..... But these will only work if the include happens at
    >the root of the webpages. The include does not work if it is called from
    >say /news/displaynews.php, since this image file is not at that point in the
    >directory tree. Again I need to use something like <img
    >src="$document_root/logo3.jpg" so it will work anywhere in the directory
    >tree.
    >
    >Is there any easy way to use the $HTTP_SERVER_VARS['DOCUMENT_ROOT'] variable
    >to create the correct path to each file ?
    >
    >Regards
    >
    >Tony
    >
    >
    >---
    >Outgoing mail is certified Virus Free.
    >Checked by AVG anti-virus system (http://www.grisoft.com).
    >Version: 6.0.778 / Virus Database: 525 - Release Date: 15/10/2004
    >
    >


  • Next message: Mike Harvey: "Re: Accessing function file on another domanin"

    Relevant Pages


    Loading