469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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_ROOT"] 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

Sep 5 '05 #1
15 6811
In article <a1***************************@news.pandora.be>,
tom pester <To********************@pandora.be> wrote:
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_ROOT"] 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 :)


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]
Sep 5 '05 #2
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

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.

Sep 5 '05 #3
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

Sep 6 '05 #4
phpWalter wrote:
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


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.
js*******@attglobal.net
==================
Sep 6 '05 #5
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
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

Sep 6 '05 #6
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
phpWalter wrote:
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

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.

Sep 6 '05 #7
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


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.
js*******@attglobal.net
==================
Sep 6 '05 #8
> (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 :)
Hmmm, it exists in my IIS setup (W2K, PHP 5.0.3).


I get this message when using it :
Notice: Undefined index: DOCUMENT_ROOT in C:\Inetpub\wwwroot\PHP\Pages\test.php
on line 2

I've tested it on :

WXP,IIS5.1,PHP 5.0.3
and
W2003server,IIS6.0,PHP 5.0.3

Cheers,
Tom Pester
Sep 6 '05 #9
tom pester wrote:
(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 :)


Sorry, I really didn't mean to imply anything. Some groups top post,
others (like this one) bottom post.
Hmmm, it exists in my IIS setup (W2K, PHP 5.0.3).

I get this message when using it :
Notice: Undefined index: DOCUMENT_ROOT in
C:\Inetpub\wwwroot\PHP\Pages\test.php on line 2

I've tested it on :

WXP,IIS5.1,PHP 5.0.3
and
W2003server,IIS6.0,PHP 5.0.3
Cheers,
Tom Pester


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.
js*******@attglobal.net
==================
Sep 7 '05 #10
> Interesting. What does phpinfo() show? And did you try
$_SERVER["APPL_PHYSICAL_PATH"]? I'm wondering if this one works.

Sep 7 '05 #11
> Interesting. What does phpinfo() show? And did you try
$_SERVER["APPL_PHYSICAL_PATH"]? I'm wondering if this one works.


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_PATH in D:\DataBackup\Projects\Websites\PHP\Pages\test.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?
Sep 7 '05 #12
tom pester wrote:
Interesting. What does phpinfo() show? And did you try
$_SERVER["APPL_PHYSICAL_PATH"]? I'm wondering if this one works.

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_PATH in
D:\DataBackup\Projects\Websites\PHP\Pages\test.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?


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.
js*******@attglobal.net
==================
Sep 7 '05 #13
> 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.


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
Sep 7 '05 #14
tom pester wrote:
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.

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


Yes, that's how I have it set up. Mainly a matter of loading
phpisapi.dll in your web site configuration (under "Administrative 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.
js*******@attglobal.net
==================
Sep 8 '05 #15
tom pester wrote:
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" ?>

^^^^^^^^^

Don't use include path like this. Better use relative path (eg,
../foo ../foo) or absolute path (eg, /foo). Usage of this will result in
ambiguous directory resolve (ie, it will start searching the existence
of path in include_path, etc) and will hit performance.

FWIW, I use something like:

<?php
//config.inc.php
$CFG['project_path'] = '/home/foo/foo/';
//etc etc
?>

<?php
//foo.php
require_once('../common/config.inc.php'); //relative to project
require_once($CFG['project_path'] . 'html/header.html'); //absolute
//etc etc...
require_once($CFG['project_path'] . 'html/footer.html'); //absolute
?>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Sep 9 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Intaek LIM | last post: by
13 posts views Thread by alex | last post: by
1 post views Thread by floatingpoint | last post: by
11 posts views Thread by cybervigilante | last post: by
2 posts views Thread by dave6502 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.