www.webdeveloper.com
Results 1 to 3 of 3

Thread: First time using include statement and not working properly!

  1. #1
    Join Date
    Oct 2013
    Posts
    9

    First time using include statement and not working properly!

    Hi all,

    Bare with me as I'm very new to php but this issue should be easy for the gurus.

    So, I have a dynamic web template which has an 'include' command reference to two files located in a folder called 'includes' at root/includes/ to create a common header and footer to all pages that this template is applied to. Within the dynamic web template, which sits in the root of the site, are the lines

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/includes/ztest_hinclude.php";
    include($path);

    ?>

    for the header and

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/includes/ztest_finclude.php";
    include($path);

    ?>

    for the footer.

    If I apply this dynamic web template to any page in the root of the site, any hyperlinks to pages contained within the referenced files to be included are correct (e.g. the link to the home page shows as root/index.php)

    However, if I create a folder in the root, call it 'myfolder' for example, save a php file into that folder and apply the dynamic web template to it, the links referenced in the two files to be included are incorrect (e.g. instead of pointing to the home page at root/index.php it points to root/myfolder/index.php which is clearly wrong).

    The two include statements above have been sourced from the web and this seems to be the acccepted method of determining the root of the site so I can't understand quite where I've gone wrong. Any advice would be much appreciated.

  2. #2
    Join Date
    Oct 2013
    Posts
    9
    Apparently the solution is not to link to index.php but /index.php so if the template is applied to any file in a sub-folder, the path to index.php is reset back to the document root.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Note that you are specifying a file system path, which means that "/includes/ztest_hinclude.php" would be relative to the root directory ("/") on the disk, not your web site root. You could have instead used:
    PHP Code:
    include $_SERVER['DOCUMENT_ROOT']."/includes/ztest_hinclude.php"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles