|
Posted by Tom Rogers on 03/05/05 06:13
Hi,
Saturday, March 5, 2005, 5:47:07 AM, you wrote:
LG> Hello Richard,
LG> Friday, March 4, 2005, 11:41:29 AM, you wrote:
R>> http://php.net/set_include_path
LG> Ok... Maybe I should put all this together in one e-mail so that all
LG> the issues can be looked at...
LG> The problem:
LG> Finding a reliable method to include files, keeping in mind the
LG> following:
LG> 1. The site could be moved to a completely new host which could be of
LG> a different OS, and/or web server software, and could either be the
LG> one and only site on that host (dedicated server), or could be a
LG> virtual host (shared server).
LG> 2. The site could remain on the same host but is required to move to a
LG> new path. i.e. from htdocs/mysite to htdocs/mynewsite
LG> 3. The web host may or may not allow the use of .htaccess (Some Sambar
LG> configurations for example).
LG> 4. The method used would not affect any other virtual hosts. Meaning,
LG> the method used must be independent for each virtual host.
LG> 5. The method used would not utilize a folder where other virtual
LG> hosts could gain access to the included file (php.ini
LG> include_path).
LG> 6. The method (and this is the important one IMHO) would not require
LG> editing "x" number of pages in a site to change some static path
LG> that was set on each page.
LG> 7. The method used would not require a dedicated "include" file in
LG> every single folder of the site that could be included because it's
LG> in the same folder as the page needing it, because those would all
LG> have to be edited to fix the path if condition 1 or 2 was met.
LG> Previously proposed solutions:
LG> 1. PHP.ini include_path
LG> This affects all virtual hosts and would require administrative
LG> overhead to prevent the owners of each virtual host from gaining
LG> access to other virtual host's include files. I suppose you could
LG> set it to something like: include_path="/php/includes" and have a
LG> separate subfolder under that for each virtual host. But since that
LG> folder is outside the web folder, there would have to be some
LG> mechanism (additional FTP account) for each person to gain access
LG> to their own include folder to add/edit/delete files in that
LG> folder. Then if the site is moved and they aren't using an
LG> include_path, you have to fix all your pages.
LG> 2. set_include_path
LG> This means if your site moves, you must edit "x" number of pages in
LG> the site to correct the path.
LG> 3. An include file in every directory to set the include path.
LG> You'd have to edit "x" number of these files to correct the path if
LG> the site moves. This would be much less work than the previous
LG> item, but it could be a lot of work on very big sites where you
LG> don't have shell accounts to do some scripted find/replace with.
LG> 4. Use the full URL to the file in the include statement.
LG> See item 2.
LG> 5. $_SERVER["DOCUMENT_ROOT"] and $_SERVER['PATH_TRANSLATED']
LG> Not always available or incorrect see
LG> mid:1778675148.20050303102623@kwikin.com
LG> I may have missed some things, and if I've misunderstood how something
LG> should work, then please let me know. I'm just looking for a more or
LG> less foolproof method which doesn't require fixing code if the site is
LG> moved. The closest I can come to it is the function I wrote but is a
LG> pain because you have to put it in every page where you need an
LG> included file. Granted, you only have to do it once, and then you're
LG> done and a site move wont affect it, but it's still kludgy if you ask
LG> me.
LG> *******************
LG> <?php
LG> function dynRoot()
LG> {
LG> $levels = substr_count($_SERVER['PHP_SELF'],"/");
LG> for ($i=0; $i < $levels - 1; $i++)
LG> {
LG> $relativeDir .= "../";
LG> }
LG> return $relativeDir;
LG> }
?>>
LG> *******************
LG> and then calling it as such:
LG> include(dynRoot() . 'includes/db_connect.php');
LG> I've had to move client sites between Sambar, Apache, IIS and Windows,
LG> Linux. Most times I've had to go in and fix include paths because one
LG> of the above solutions were originally used and wasn't viable on the
LG> new host.
LG> Thanks.
LG> --
LG> Leif (TB lists moderator and fellow end user).
LG> Using The Bat! 3.0.2.3 Rush under Windows XP 5.1
LG> Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB
This will set the include path just before the document root:
if(isset($_SERVER["SCRIPT_FILENAME"])){
$root = $_SERVER['SCRIPT_FILENAME'];
//echo "Root: $root<br>";
$script = $_SERVER['SCRIPT_NAME'];
$document_root = str_replace($script,'',$root);
//echo "Document root: $document_root<br>";
$base = basename($document_root);
//echo "Base: $base<br>";
$include = str_replace($base,'include',$document_root);
//echo "Include: $include<br>";
$os = strtolower(PHP_OS);
//echo "OS: $os<br>";
$lk = ':';
$org_include = ini_get("include_path");
if(preg_match('/^win/i',$os)) $lk = ';';
ini_set("include_path",$include.$lk.$org_include);
//echo "Include: ".ini_get("include_path")."<br>";
}
--
regards,
Tom
[Back to original message]
|