The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Help with PHP includes?

MagicPrimeMagicPrime FiresideWizardRegistered User regular
I am trying to put together a web site using some basic PHP code. This is the first time I've done PHP so I'm taking my time, but I need some help with include files.

Here is the scenario:

In the main directory of my website I have the index.php (ofcourse) along with a lot of sub-directories.
One of these sub directories I have named "zz_include" and I want this directory to house all the include files for my web site.

As far as the index.php goes, all I have to do it for the include file is --
[PHP]<?php include("/zz_include/header.php"); ?>[/PHP]
and it works fine and dandy.

However, in a sub directory. For example, an "About" page in a directory called "about" I have to back step the include code like so:
[PHP]<?php include("../zz_include/header.php"); ?>[/PHP]
for it to work.

In an ASP setting I can do a virtual include and it will start from the highest directory in the site and find the zz_include directory.

So how can I set up my php site so that I can have one consolodated directory for my includes and use the same string of code to link to them no matter where the page is located? (So if the page is 3 levels deep I dont have to ../../..)

BNet • magicprime#1430 | PSN/Steam • MagicPrime | Origin • FireSideWizard
Critical Failures - Havenhold CampaignAugust St. Cloud (Human Ranger)
MagicPrime on

Posts

  • InfidelInfidel Heretic Registered User regular
    edited April 2009
    Two choices work well, depending on whether you can edit your php settings sufficiently.

    a) Add your document root to your include_path in php.ini/.htaccess/etc.

    b) Include the following line at the top of your scripts:
    set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] );
    

    Then if you have say in your websites root directory a folder called inc, you can include('inc/File.php') and you're set.

    Infidel on
    OrokosPA.png
  • xzzyxzzy Registered User regular
    edited April 2009
    It should work. "/" (or root directory) is going to be the same for every php script on a server.

    You are running php in some kind of chroot or secure environment? Because the root directory is supposed to be the host operating system's root (C:\ under windows, / under unix). Not really relevant to this problem, but with a default php config, "/zz_include" would be equivalent to "C:\zz_include\".

    You may be able to accomplish what you want by looking at the $_SERVER variable. This will reveal the path configured in httpd for the 'base' directory of the website. So, something like:
    $inc = $_SERVER['DOCUMENT_ROOT'] . '/zz_include';
    

    Should get you the path you want for any script.

    xzzy on
  • MagicPrimeMagicPrime FiresideWizard Registered User regular
    edited April 2009
    Infidel wrote: »
    Two choices work well, depending on whether you can edit your php settings sufficiently.

    a) Add your document root to your include_path in php.ini/.htaccess/etc.

    b) Include the following line at the top of your scripts:
    set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] );
    
    Then if you have say in your websites root directory a folder called inc, you can include('inc/File.php') and you're set.

    Awesome.

    Since I don't have direct access to the web server to do any editing or re-adjusting of the settings. That code string has solved my problems. Thanks a ton!

    MagicPrime on
    BNet • magicprime#1430 | PSN/Steam • MagicPrime | Origin • FireSideWizard
    Critical Failures - Havenhold CampaignAugust St. Cloud (Human Ranger)
Sign In or Register to comment.