By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,718 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,718 IT Pros & Developers. It's quick & easy.

Include() & Paths - Best Practices

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

http://www.tonymarston.net

"JStrummer" <go***********@jstrummer.e4ward.com> wrote in message
news:13*************************@posting.google.co m...
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.

Jul 17 '05 #2

P: n/a
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.
Jul 17 '05 #3

P: n/a
Put something like this into your .htaccess file:

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

--
Tony Marston

http://www.tonymarston.net

"JStrummer" <go***********@jstrummer.e4ward.com> wrote in message
news:13**************************@posting.google.c om...
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.

Jul 17 '05 #4

P: n/a
"JStrummer" <go***********@jstrummer.e4ward.com> wrote in message
news:13*************************@posting.google.co m...
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").
In my opinion, given that your application has a defined directory
structure, I'd always use relative paths.
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?
$_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.
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.


I'm a relative path proponent....

- Virgil
Jul 17 '05 #5

P: n/a
JStrummer wrote:
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


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)
http://www.tsmchughs.com/
Jul 17 '05 #6

P: n/a
go***********@jstrummer.e4ward.com (JStrummer) wrote in message news:<13*************************@posting.google.c om>...
I have a question regarding paths and the include() statement in PHP.


<snip>

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

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.