Forum Moderators: coopster
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.
- 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?
# 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,
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;"
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)?