Results 1 to 3 of 3

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

Hybrid View

  1. #1
    Join Date
    Oct 2013

    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

    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/includes/ztest_hinclude.php";


    for the header and

    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/includes/ztest_finclude.php";


    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
    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
    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"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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