Include() & Paths - Best Practices

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

    Include() & Paths - Best Practices

    I have a question regarding paths and the include() statement in PHP.

    I develop in a Windows environment and will be publishing to a Linux
    server. I would like to do the following:

    1. Setup my include references in such a way that I don't have to
    change them all every time I have to publish to the production server

    2. Setup above in such a way that won't involve php.ini (& LInux
    equivalent), as I have access to edit this file locally, but won't be
    able to do so in the production environment

    My file structure is such:

    wwwroot
    subdir
    global
    inc

    I tried Windows-styled virtual references such as "/" to refer to the
    wwwroot, but that didn't work in the production evironment. I switched
    to relative paths (eg include("global/inc/config.inc") or include
    ("../global/inc/config").

    I guess what I'm looking for here is a best practice for how to set
    the includes up for maximum portability across servers with different
    file systems as well as different platforms. Can you, for instance,
    incorporate a variable that defines the root path into the include
    statement above? If so, where do you define this variable globally
    without having to add to every file? (b/c you can't use an include
    statement if you haven't gotten the includes to work) Is there an
    equivalent of global.asa for the PHP world?

    I'm just raising some issues with my experience thus far. I know
    there's a solution and a best practice that I'm just missing to the
    point--I would appreciate any wisdom that could be shared.
  • Tony Marston

    #2
    Re: Include() & Paths - Best Practices

    I have different include paths on my development PC (Windows XP) and my
    production server (Linux). I use the php.ini file on my development PC, but
    on the Linux server I have an entry in the .htaccess file which sets a
    different set of path names. In this way none of my PHP scripts has to
    *know* what set of include paths to use, so I do not have to change anything
    when copying files from my PC to my server.

    --
    Tony Marston

    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




    "JStrummer" <google-usenet@jstrumme r.e4ward.com> wrote in message
    news:137012e7.0 405210723.71702 f6@posting.goog le.com...[color=blue]
    > I have a question regarding paths and the include() statement in PHP.
    >
    > I develop in a Windows environment and will be publishing to a Linux
    > server. I would like to do the following:
    >
    > 1. Setup my include references in such a way that I don't have to
    > change them all every time I have to publish to the production server
    >
    > 2. Setup above in such a way that won't involve php.ini (& LInux
    > equivalent), as I have access to edit this file locally, but won't be
    > able to do so in the production environment
    >
    > My file structure is such:
    >
    > wwwroot
    > subdir
    > global
    > inc
    >
    > I tried Windows-styled virtual references such as "/" to refer to the
    > wwwroot, but that didn't work in the production evironment. I switched
    > to relative paths (eg include("global/inc/config.inc") or include
    > ("../global/inc/config").
    >
    > I guess what I'm looking for here is a best practice for how to set
    > the includes up for maximum portability across servers with different
    > file systems as well as different platforms. Can you, for instance,
    > incorporate a variable that defines the root path into the include
    > statement above? If so, where do you define this variable globally
    > without having to add to every file? (b/c you can't use an include
    > statement if you haven't gotten the includes to work) Is there an
    > equivalent of global.asa for the PHP world?
    >
    > I'm just raising some issues with my experience thus far. I know
    > there's a solution and a best practice that I'm just missing to the
    > point--I would appreciate any wisdom that could be shared.[/color]


    Comment

    • JStrummer

      #3
      Re: Include() &amp; Paths - Best Practices

      Tony-

      What would I need to add to the .htaccess file? Is it different syntax
      than standard PHP? I would appreciate any guidance here. Thanks.

      Comment

      • Tony Marston

        #4
        Re: Include() &amp; Paths - Best Practices

        Put something like this into your .htaccess file:

        php_value include_path ".:/usr/local/lib/php:/path2;/path3"

        --
        Tony Marston

        This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




        "JStrummer" <google-usenet@jstrumme r.e4ward.com> wrote in message
        news:137012e7.0 405211410.14044 7f8@posting.goo gle.com...[color=blue]
        > Tony-
        >
        > What would I need to add to the .htaccess file? Is it different syntax
        > than standard PHP? I would appreciate any guidance here. Thanks.[/color]


        Comment

        • Virgil Green

          #5
          Re: Include() &amp; Paths - Best Practices

          "JStrummer" <google-usenet@jstrumme r.e4ward.com> wrote in message
          news:137012e7.0 405210723.71702 f6@posting.goog le.com...[color=blue]
          > I have a question regarding paths and the include() statement in PHP.
          >
          > I develop in a Windows environment and will be publishing to a Linux
          > server. I would like to do the following:
          >
          > 1. Setup my include references in such a way that I don't have to
          > change them all every time I have to publish to the production server
          >
          > 2. Setup above in such a way that won't involve php.ini (& LInux
          > equivalent), as I have access to edit this file locally, but won't be
          > able to do so in the production environment
          >
          > My file structure is such:
          >
          > wwwroot
          > subdir
          > global
          > inc
          >
          > I tried Windows-styled virtual references such as "/" to refer to the
          > wwwroot, but that didn't work in the production evironment. I switched
          > to relative paths (eg include("global/inc/config.inc") or include
          > ("../global/inc/config").[/color]

          In my opinion, given that your application has a defined directory
          structure, I'd always use relative paths.
          [color=blue]
          > I guess what I'm looking for here is a best practice for how to set
          > the includes up for maximum portability across servers with different
          > file systems as well as different platforms. Can you, for instance,
          > incorporate a variable that defines the root path into the include
          > statement above? If so, where do you define this variable globally
          > without having to add to every file? (b/c you can't use an include
          > statement if you haven't gotten the includes to work) Is there an
          > equivalent of global.asa for the PHP world?[/color]

          $_SERVER['DOCUMENT_ROOT'] will return the document root. Using by itself,
          however, means that you may only install your application at a known point
          relative to the web root. I might use separate domain names on my home
          machine with separate web roots, but I might move code to publicly
          accessible server where I put all code under the same domain but under a
          separate directory within that domain. If all paths are relative, you can
          make this kind of move freely.

          You could add dirname(__FILE_ _) to the mix to get the full path to the
          directory in which your index.php file is located (provided you put that
          *in* your index.php... and index.php is your default file in your
          applicaiton) and then assign that to a variable you always use concatenated
          with the relative file/path you want... but then you might as well just use
          relative paths IMO.
          [color=blue]
          > I'm just raising some issues with my experience thus far. I know
          > there's a solution and a best practice that I'm just missing to the
          > point--I would appreciate any wisdom that could be shared.[/color]

          I'm a relative path proponent....

          - Virgil


          Comment

          • Brian

            #6
            Re: Include() &amp; Paths - Best Practices

            JStrummer wrote:
            [color=blue]
            > I develop in a Windows environment and will be publishing to a Linux
            > server. I would like to do the following:
            >
            > 1. Setup my include references in such a way that I don't have to
            > change them all every time I have to publish to the production server[/color]

            The best solution is to install your own web server. I'm assuming your
            Linux-based server uses Apache? Download a copy -- it's free -- and
            install it. Just follow some guidelines for security (e.g., only listen
            to 127.0.0.1). Add the PHP module. Now you've got a setup that is almost
            a mirror of what is live.

            --
            Brian (remove "invalid" from my address to email me)

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: Include() &amp; Paths - Best Practices

              google-usenet@jstrumme r.e4ward.com (JStrummer) wrote in message news:<137012e7. 0405210723.7170 2f6@posting.goo gle.com>...[color=blue]
              > I have a question regarding paths and the include() statement in PHP.[/color]

              <snip>

              I use relative path like include('../../foo/foo.php'); working fine all the time.

              --
              | Just another PHP saint |
              Email: rrjanbiah-at-Y!com

              Comment

              Working...