Forum Moderators: coopster

Message Too Old, No Replies

Controling include path using config.php

(Fundamental idea needs clarification...)

         

penders

10:20 pm on Sep 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In implementing something like what coopster suggests in the Forum Library [webmasterworld.com] to simplify the inclusion of files...

Wouldn't it be nice to just include() your files and never have to worry about the path? You set it once when you setup a new site and be done with it. A lot of programmers use this technique, you'll often see it called the "config.php" script or something similar. Here's a sample to get you started (replace the bold path with the path to your includes directory):

ini_set('include_path',  
realpath($_SERVER['DOCUMENT_ROOT'] . '/../includes/')
. PATH_SEPARATOR
. ini_get('include_path'));

But where do you keep 'config.php' and how do you include it?

(1) Copy config.php into each folder you need to include files?! And simply include("config.php") ?
- But I really don't want to duplicate config.php, that kinda defeats the object!?

(2) Have one copy of config.php in the webroot, or a fixed sub directory and include($_SERVER["DOCUMENT_ROOT"].'/config.php') ?
- But I want to be able to test the code on Windows/IIS (as well as Linux/Apache), so $_SERVER["DOCUMENT_ROOT"] might not be available!?

(3) A better way?

Is there a better way? I seem to be struggling with what appears to be quite a fundamental idea! :/

Thanks.

penders

9:30 am on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



- But I want to be able to test the code on Windows/IIS (as well as Linux/Apache), so $_SERVER["DOCUMENT_ROOT"] might not be available!?

Ok, I could auto_prepend_file a short script to check if $_SERVER["DOCUMENT_ROOT"] exists and work it out if it doesn't. Then do (2) above. (Hey, I could just auto_prepend_file the config.php script itself, but I'm not sure if I want to go that far?)

However... although on my Windows/IIS test server I have access to php.ini in order to add the auto_prepend_file directive, how do I do this on a shared server? Apache has .htaccess, how can I do this on IIS?

g1smd

9:42 am on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could set it as the auto-prepend file, but I do something different. I add this to the .htaccess file:

# Default Folder for 'PHP Include' Files:

# For (www.)domain.com only:
php_value include_path /vhost/vhost4/d/o/m5/domain.com/www/includes

# For (www.)test.domain.com and localhost and 127.0.0.1:
# php_value include_path /apache/htdocs/localtest.domain.com/includes

I just uncomment out one line and comment the other when uploading the site copy from the local test server to the live site hosting,

penders

10:24 am on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the suggestion. Although how do you get this to work on IIS / Windows?

Also, in coopsters example above, the original include_path is appended at the end, "In case you want to use any existing paths, such as PEAR classes."

coopster

1:37 pm on Sep 22, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not certain how to use per-directory overrides on IIS. You may have to ask that question in the Microsoft IIS Web Server and ASP.NET forum [webmasterworld.com].

As far as the directive goes, you can still use existing paths by writing them into your per-directory override as g1smd shows. Just append them, separating each with your appropriate directory separator. For Windows, that might look something like this:

php_value include_path "C:\www\includes;.;C:\php\PEAR;"

penders

10:03 am on Sep 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not certain how to use per-directory overrides on IIS. You may have to ask that question in the Microsoft IIS Web Server and ASP.NET forum.

This appears to be a common query, with no simple answer as far as I can see? Configuration changes can be made in the IIS console, but for a shared host that's a bit tricky. To be honest, for me this is becoming more of just theoretical interest as I don't need to use IIS at the mo.

php_value include_path "C:\www\includes;.;C:\php\PEAR;"

By this method, is there any way to append to the current include_path - whatever that might be (assuming this is unknown... perhaps if incorporating a generic script)?

coopster

1:04 pm on Sep 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not that I am aware of. That's why you see so many open source packages use configuration scripts at the root/base of the application and then include that one from each application driver script.