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 13 of 13

Thread: php include

  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php include

    hi all,

    im looking for a bit of php help with the include thing. im making a site which is going to have about 20+ pages and the menu/navigation will be exactly the same on each page.. but i dont want to have to edit every single page just to change 1link in the menu.

    Ive been reading up on how i could do this and ive worked out how to do it partly.

    i use this for the index page ,
    Code:
    <?PHP
     include("menu.htm");
    ?>
    and that seems to work just fine.

    But when i try to add the menu to a page in another directorie, for example, this page, it just gives me aload of errors.

    could any body tell me how to fix this please, thanks
    steve

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    I'm guessing that your include is not pointing to the correct directory.

    You need to do something like

    Code:
    <?
    include ("../../menu.htm")
    ?>
    or use an absolute reference such as

    Code:
    <?
    include ("http://www.lil9.com/menu.htm")
    ?>
    that should fix it.

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm neither of those made any difference

    any other ideas?

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    PHP Code:
    @readfile('menu.htm'); 

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try it this way:

    PHP Code:
    include('/home/lil9/public_html/menu.htm'); 

  • #6
    New Coder
    Join Date
    Jun 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nope still no luck sadly

    i tried both those but it just brings up errors again.

    does the menu HAVE to be in the same folder as the page that i want it to display on?

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope, if you use the format I gave you then you can have the menu file anywhere you like and just point to it from the various other directories. The tough part is that servers can be configured differently to have different paths and you've gotta figure out the right path for your server.

    Try this method, create a php file with the following content, nothing else:

    PHP Code:
    <?php
    echo realpath('.');
    ?>
    Put this file in the same directory as your menu file then go to the php file in your browser. It will tell you exactly the server path to the directory in which your menu file resides. Then just use that in your include statement using the format I gave you above. For instance, if the path says the following:

    PHP Code:
    /usr/home/lil9/public_html 
    Then you would create an include like this:

    PHP Code:
    include('/usr/home/lil9/public_html/menu.htm'); 

  • #8
    Senior Coder JamieR's Avatar
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    3,161
    Thanks
    0
    Thanked 5 Times in 5 Posts
    That's a bit on the weird side. An absolute url in an include has always worked for me, however I'm thinking that it must be down to the way that this guy's server is set up

  • #9
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it in a subdirectory from the page you want to include?
    if so:
    PHP Code:
    <?php
    include('/directory_name/file.php');
    ?>
    if it's one directory up:
    PHP Code:
    <?php
    include('../file.php');
    ?>

    Also, dont use a absolute url to much.
    you can use $_SERVER['DOCUMENT_ROOT']

  • #10
    New Coder
    Join Date
    Mar 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by lil9
    nope still no luck sadly

    i tried both those but it just brings up errors again.

    does the menu HAVE to be in the same folder as the page that i want it to display on?
    No, your include files can be anywhere in your directory tree. You just have to know how to reference them.

    This is just an example of how you could set your directory structure up, off the root folder:

    ROOT
    - /js - Javascripts
    - /images - Images
    - /includes - For all included files
    - /etc, etc, etc - You get the picture.

    As you develop your pages, call your included file based on where the calling file is and where the included file is. For example, lets says you are in a folder called /test and you are including a file from /includes. I would do something along the lines of this...
    PHP Code:

    // I need to go down a folder to get to root
    $page_root_path "./../";

    // Now I need to grab a file to include from the includes folder
    include_once($page_root_path "includes/file_to_included.php"); 
    This should work just about anyhere in your scripts as long as you tell the $page_root_path var where you are in relationship to the root folder.

  • #11
    New Coder
    Join Date
    Jun 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sounds good, il give that a try

  • #12
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weazel
    That's a bit on the weird side. An absolute url in an include has always worked for me, however I'm thinking that it must be down to the way that this guy's server is set up
    Most likely URL fopen wrappers are disabled in their PHP config. By default they're enabled so url formatted includes work but it's good practice not to use them if you want the code to be fully portable, an absolute server path works just as well and isn't config dependent

  • #13
    New Coder
    Join Date
    Jun 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for all your help guys, its working now!


    thanks again


  •  

    Posting Permissions

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