How to make the include path universal?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tom pester

    How to make the include path universal?

    Hi,

    Im new to php and have a background in asp. All the help is very much appreciated.

    today I was using the include function in a file that itself gets included :

    ***************
    consumer.php : <? include "inc.php" ?>

    inc.php : <? include "Common/functions.php" ?>

    functions.php : common functions that all pages use
    ***************

    This works fine if the path that's specified in
    <? include "Common/functions.php" ?>
    is relative to the path of the calling page consumer.php

    I want to be able to specify <? include "inc.php" ?> in every page no matter
    if its nested in a directory or not.

    In asp I would use absolute paths like this : /Common/functions.php and it
    would work.
    PHP comes close to it when using paths like this : c:/website/site1/Common/functions.php

    But thus breaks when I transfer the pages from the development server to
    the live server cause there is no c:/website/site1

    My next guess was that I could solve this with a variable like this : $docroot
    .. "/Common/functions.php" but where do I specify $docroot in a central location?
    (I would do this in asp's global.asa)

    _SERVER["DOCUMENT_R OOT"] is blank when I request it and, if I understand
    it correclty, I can set it in the php.ini . The problem is this value holds
    for all the sites on the box.

    I'm new to php so if I don't make sense than pls put me straight :)

    Cheers,
    Tom Pester



  • Sandman

    #2
    Re: How to make the include path universal?

    In article <a1a977a213f4a8 8c780622e40e800 @news.pandora.b e>,
    tom pester <Tom.PesterDELE TETHISSS@pandor a.be> wrote:
    [color=blue]
    > Hi,
    >
    > Im new to php and have a background in asp. All the help is very much
    > appreciated.
    >
    > today I was using the include function in a file that itself gets included :
    >
    > ***************
    > consumer.php : <? include "inc.php" ?>
    >
    > inc.php : <? include "Common/functions.php" ?>
    >
    > functions.php : common functions that all pages use
    > ***************
    >
    > This works fine if the path that's specified in
    > <? include "Common/functions.php" ?>
    > is relative to the path of the calling page consumer.php
    >
    > I want to be able to specify <? include "inc.php" ?> in every page no matter
    > if its nested in a directory or not.
    >
    > In asp I would use absolute paths like this : /Common/functions.php and it
    > would work.
    > PHP comes close to it when using paths like this :
    > c:/website/site1/Common/functions.php
    >
    > But thus breaks when I transfer the pages from the development server to
    > the live server cause there is no c:/website/site1
    >
    > My next guess was that I could solve this with a variable like this :
    > $docroot
    > . "/Common/functions.php" but where do I specify $docroot in a central
    > location?
    > (I would do this in asp's global.asa)
    >
    > _SERVER["DOCUMENT_R OOT"] is blank when I request it and, if I understand
    > it correclty, I can set it in the php.ini . The problem is this value holds
    > for all the sites on the box.
    >
    > I'm new to php so if I don't make sense than pls put me straight :)[/color]

    You can specify the include_path in php.ini to a path shared by all
    environments.

    include_path on your dev machine could be c:/websites/includes/ and on your
    server c:/includes/ and keep them in sync.



    --
    Sandman[.net]

    Comment

    • tom pester

      #3
      Re: How to make the include path universal?

      Hi Sandman,

      Reading further upon the subject I found your solution.

      I have some problems with it though :

      - There is only 1 php.ini for all the sites on the server so I will end up
      putting all includes of different projects in 1 directory (I could prepend
      the include file with a project prefix to avoid naming collisions)
      - Or I could expand include_path and add a directory per site (naming collisions
      are possible again so I have to adopt a naming scheme)
      - I read that some isp's don't allow editing the php.ini (especialy if you
      are using a shared server, there is only 1 php.ini right?)
      - The site isn't self contained. I can't xcopy 1 directory to deploy are
      backup the site. Information is scatered around the system.

      disclaimer :
      This are all comments of a php beginner so _please_ correct me if I'm wrong.

      Cheers,
      Tom Pester

      [color=blue]
      > You can specify the include_path in php.ini to a path shared by all
      > environments.
      >
      > include_path on your dev machine could be c:/websites/includes/ and on
      > your server c:/includes/ and keep them in sync.
      >[/color]


      Comment

      • phpWalter

        #4
        Re: How to make the include path universal?

        We solve the prolbem by defining a base "vars.php" file at the "root"
        of the site.

        In there we define paths to constants, and then these constants are
        used to INCLUDE files as needed.


        var.php

        define( INCLUDE_PATH, '/my/path/includes/');

        now I can incluse my files as Ineed

        include_once INCLUDE_PATH . 'base_functions .php';


        All you need to do is update that one constant as you move from site to
        site and it works fine.

        Linux or windows.

        walter

        Comment

        • Jerry Stuckle

          #5
          Re: How to make the include path universal?

          phpWalter wrote:[color=blue]
          > We solve the prolbem by defining a base "vars.php" file at the "root"
          > of the site.
          >
          > In there we define paths to constants, and then these constants are
          > used to INCLUDE files as needed.
          >
          >
          > var.php
          >
          > define( INCLUDE_PATH, '/my/path/includes/');
          >
          > now I can incluse my files as Ineed
          >
          > include_once INCLUDE_PATH . 'base_functions .php';
          >
          >
          > All you need to do is update that one constant as you move from site to
          > site and it works fine.
          >
          > Linux or windows.
          >
          > walter
          >[/color]

          Walter,

          Looks like unnecessary work to me. And what happens if the root
          directory of the site is moved? Or you need to use the same pages on
          another server? Everything breaks until you change your var.php.

          I'd rather use something like:

          include_once($_ SERVER['DOCUMENT_ROOT'].'/includes/base_functions. php');

          This changes automatically based on your Apache document root setting.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • tom pester

            #6
            Re: How to make the include path universal?

            Hi Walter

            This is a good solution that AFAIK minimizes the poblem cause you still have
            to include var.php in each file.
            And the path to the var.php suffers the same problems now but it's only here
            that the problem surfaces.

            **** code ****
            include_once 'var.php'; // You didn't mention this line but its required
            in every file right?
            include_once INCLUDE_PATH . 'base_functions .php';
            **** code end ****

            the
            include_once 'var.php'
            is not universal for each file since it breaks if the file that includes
            it is in another directory, fe ;

            include_once 'var.php'
            becomes
            include_once '../var.php'

            For the rest its a fine solution. Can you conform that's how you ment it?
            I'm still learning the details of the language..

            Cheers,
            Tom Pester
            [color=blue]
            > We solve the prolbem by defining a base "vars.php" file at the "root"
            > of the site.
            >
            > In there we define paths to constants, and then these constants are
            > used to INCLUDE files as needed.
            >
            > var.php
            >
            > define( INCLUDE_PATH, '/my/path/includes/');
            >
            > now I can incluse my files as Ineed
            >
            > include_once INCLUDE_PATH . 'base_functions .php';
            >
            > All you need to do is update that one constant as you move from site
            > to site and it works fine.
            >
            > Linux or windows.
            >
            > walter
            >[/color]


            Comment

            • tom pester

              #7
              Re: How to make the include path universal?

              Hi Jerry,

              Using $_SERVER['DOCUMENT_ROOT'] is indd the standard php way but my problem
              is that it's not defined on a windows IIS box (I confirmed this by reading
              previous posts).

              Cheers,
              Tom Pester
              [color=blue]
              > phpWalter wrote:
              >[color=green]
              >> We solve the prolbem by defining a base "vars.php" file at the "root"
              >> of the site.
              >>
              >> In there we define paths to constants, and then these constants are
              >> used to INCLUDE files as needed.
              >>
              >> var.php
              >>
              >> define( INCLUDE_PATH, '/my/path/includes/');
              >>
              >> now I can incluse my files as Ineed
              >>
              >> include_once INCLUDE_PATH . 'base_functions .php';
              >>
              >> All you need to do is update that one constant as you move from site
              >> to site and it works fine.
              >>
              >> Linux or windows.
              >>
              >> walter
              >>[/color]
              > Walter,
              >
              > Looks like unnecessary work to me. And what happens if the root
              > directory of the site is moved? Or you need to use the same pages on
              > another server? Everything breaks until you change your var.php.
              >
              > I'd rather use something like:
              >
              > include_once($_ SERVER['DOCUMENT_ROOT'].'/includes/base_functions. php')
              > ;
              >
              > This changes automatically based on your Apache document root setting.
              >[/color]


              Comment

              • Jerry Stuckle

                #8
                Re: How to make the include path universal?

                [color=blue]
                > Hi Jerry,
                >
                > Using $_SERVER['DOCUMENT_ROOT'] is indd the standard php way but my
                > problem is that it's not defined on a windows IIS box (I confirmed this
                > by reading previous posts).
                >
                > Cheers,
                > Tom Pester
                >[/color]

                Tom,

                (please don't top post).

                Hmmm, it exists in my IIS setup (W2K, PHP 5.0.3).

                Of course, there's always $_SERVER["APPL_PHYSICAL_ PATH"] which also
                seems to contain the path to the document root. But I haven't used it.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • tom pester

                  #9
                  Re: How to make the include path universal?

                  > (please don't top post).

                  "This term is generally used pejoratively with the implication that the offending
                  person is a newbie, a Microsoft addict (Microsoft mail tools produce a similar
                  format by default), or simply a common-and-garden-variety idiot."

                  Will do Jerry :)
                  [color=blue]
                  > Hmmm, it exists in my IIS setup (W2K, PHP 5.0.3).[/color]

                  I get this message when using it :
                  Notice: Undefined index: DOCUMENT_ROOT in C:\Inetpub\wwwr oot\PHP\Pages\t est.php
                  on line 2

                  I've tested it on :

                  WXP,IIS5.1,PHP 5.0.3
                  and
                  W2003server,IIS 6.0,PHP 5.0.3

                  Cheers,
                  Tom Pester


                  Comment

                  • Jerry Stuckle

                    #10
                    Re: How to make the include path universal?

                    tom pester wrote:[color=blue][color=green]
                    >> (please don't top post).[/color]
                    >
                    >
                    > "This term is generally used pejoratively with the implication that the
                    > offending person is a newbie, a Microsoft addict (Microsoft mail tools
                    > produce a similar format by default), or simply a
                    > common-and-garden-variety idiot."
                    >
                    > Will do Jerry :)[/color]

                    Sorry, I really didn't mean to imply anything. Some groups top post,
                    others (like this one) bottom post.
                    [color=blue]
                    >[color=green]
                    >> Hmmm, it exists in my IIS setup (W2K, PHP 5.0.3).[/color]
                    >
                    >
                    > I get this message when using it :
                    > Notice: Undefined index: DOCUMENT_ROOT in
                    > C:\Inetpub\wwwr oot\PHP\Pages\t est.php on line 2
                    >
                    > I've tested it on :
                    >
                    > WXP,IIS5.1,PHP 5.0.3
                    > and
                    > W2003server,IIS 6.0,PHP 5.0.3
                    > Cheers,
                    > Tom Pester
                    >
                    >[/color]

                    Interesting. What does phpinfo() show? And did you try
                    $_SERVER["APPL_PHYSICAL_ PATH"]? I'm wondering if this one works.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • tom pester

                      #11
                      Re: How to make the include path universal?

                      > Interesting. What does phpinfo() show? And did you try[color=blue]
                      > $_SERVER["APPL_PHYSICAL_ PATH"]? I'm wondering if this one works.[/color]


                      Comment

                      • tom pester

                        #12
                        Re: How to make the include path universal?

                        > Interesting. What does phpinfo() show? And did you try[color=blue]
                        > $_SERVER["APPL_PHYSICAL_ PATH"]? I'm wondering if this one works.[/color]

                        I can't find the the term DOCUMENT_ROOT in phpInfo() but I do find doc_root :

                        doc_root no value no vlaue

                        If I try
                        print $_SERVER["APPL_PHYSICAL_ PATH"]
                        i get
                        Notice: Undefined index: APPL_PHYSICAL_P ATH in D:\DataBackup\P rojects\Website s\PHP\Pages\tes t.php
                        on line 3

                        What's going on on my 2 machines?? These are basiacly 2 clean installs so
                        I doubt its a config issue on my part.

                        Can somebody else confirm that
                        $_SERVER['DOCUMENT_ROOT']
                        $_SERVER["APPL_PHYSICAL_ PATH"]

                        Are empty on some windows machines?


                        Comment

                        • Jerry Stuckle

                          #13
                          Re: How to make the include path universal?

                          tom pester wrote:[color=blue][color=green]
                          >> Interesting. What does phpinfo() show? And did you try
                          >> $_SERVER["APPL_PHYSICAL_ PATH"]? I'm wondering if this one works.[/color]
                          >
                          >
                          > I can't find the the term DOCUMENT_ROOT in phpInfo() but I do find
                          > doc_root :
                          >
                          > doc_root no value no vlaue
                          >
                          > If I try print $_SERVER["APPL_PHYSICAL_ PATH"] i get
                          > Notice: Undefined index: APPL_PHYSICAL_P ATH in
                          > D:\DataBackup\P rojects\Website s\PHP\Pages\tes t.php on line 3
                          >
                          > What's going on on my 2 machines?? These are basiacly 2 clean installs
                          > so I doubt its a config issue on my part.
                          >
                          > Can somebody else confirm that $_SERVER['DOCUMENT_ROOT']
                          > $_SERVER["APPL_PHYSICAL_ PATH"]
                          >
                          > Are empty on some windows machines?
                          >
                          >[/color]

                          Tom,

                          Hmm, I'm not sure what the differences are. I've got a basic
                          installation of PHP on IIS on my end, also. Are you by any chance using
                          the CGI version of PHP? I'm using the isapi version here.

                          I also have Apache installed on this system, and both share the same
                          ..ini file. But I don't think that should affect anything. I tried
                          stopping Apache, but no difference - both values are there.

                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===

                          Comment

                          • tom pester

                            #14
                            Re: How to make the include path universal?

                            > Hmm, I'm not sure what the differences are. I've got a basic[color=blue]
                            > installation of PHP on IIS on my end, also. Are you by any chance
                            > using the CGI version of PHP? I'm using the isapi version here.[/color]

                            aha! I use php-cgi.exe so that could be it.

                            Can you set up php 5.03 so that it uses isapi? I will test it once I get
                            the chance.

                            Cheers,
                            Tom Pester


                            Comment

                            • Jerry Stuckle

                              #15
                              Re: How to make the include path universal?

                              tom pester wrote:[color=blue][color=green]
                              >> Hmm, I'm not sure what the differences are. I've got a basic
                              >> installation of PHP on IIS on my end, also. Are you by any chance
                              >> using the CGI version of PHP? I'm using the isapi version here.[/color]
                              >
                              >
                              > aha! I use php-cgi.exe so that could be it.
                              >
                              > Can you set up php 5.03 so that it uses isapi? I will test it once I get
                              > the chance.
                              >
                              > Cheers,
                              > Tom Pester
                              >
                              >[/color]

                              Yes, that's how I have it set up. Mainly a matter of loading
                              phpisapi.dll in your web site configuration (under "Administra tive Tools
                              -> Computer Management) and setting it up to take .php files.

                              It took me a few tries to get it right and I don't remember exactly all
                              I had to do - but I'm sure you can find a tutorial for it on the web
                              someplace.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...