Re: best way to properly build an include path *regardless* from where I am calling the include?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 5 Jul 2009 14:33:07 -0600, govinda.webdnatalk@xxxxxxxxx (Govinda) wrote:

>I am confusing myself reading the docs just now.
>
>i.e.:
>include_path
>basename()
>and dirname()
>
>  I had thought from many months ago that
><?php include '/somedir/somefile.php'; ?>
>would include
>somefile.php
>living in
>somedir
>regardless from where in the site structure I am calling it.
>
>Now it does not seem to be doing that.
>
>What is the safest (portable) way to properly build an include path  
>*regardless* from where in my site I am calling the include?
>If I understand right I can put includes in a dir/ specified by the  
>include_path setting, but I also want to know how to do this from just  
>within the root of my virtual server.

I have developed a quite sophisticated development system, which enables me to have
several different websites using the same software, and allows them all to share the same
utilities. I know some of you don't like some of the methods I use, but I think the
general idea is relevant to this discussion.

Each website has a local directory:
D:Websites/Website_1
D:Websites/Website_2
etc.
I also have an independent directory containing the common utilities for all the websites:
D:Utilities

Each website has one or more divisions, corresponding very roughly to photo albums. It
also has one directory containing the developmental version of the program, and a second
containing the working version of the program e.g.
	Dev
	Wkg
	Album_1
	etc.
The remote version of each website only has the directories:
	Wkg
	Utilities
	Album_1
	etc.

a new web page is invoked with the command: 
http://localhost/Website_1/index.php
or
http://www.corybas.com/index.php

If I want to ensure that a fresh version is loaded I add the parameter: ?new=1, and if I
want to invoke the developmental version, I followed this with the parameter:
&local_dir=Dev.

In either case the program index.php is loaded. This has the following code:

<?php
	$wkg_dir = 'Wkg'; $dev_dir = 'Dev'; // Default working version
	$program = 'Mailmerge.php';					// Name of main
program 
	$new = false; 
	if (isset($_GET['new']))  	{ $new = $_GET['new'];  }	
if ($new == 1)			// Unset $local_dir if 'New' set
		{ if (isset($_SESSION['local_dir']))  { unset($_SESSION['local_dir']); }
		} 

	if (!isset($_SESSION['local_dir'])) // Set up session variable 'local_dir' 
		{ $_SESSION['local_dir'] = $wkg_dir; }
	$local_dir = &$_SESSION['local_dir'];

	if (isset($_GET['local_dir'])) 		// Get local_dir, if passed as parameter
		{ $local_dir = $_GET['local_dir'];  }
	$local_dir = (rtrim($local_dir, "/")); // Verify valid directory
//	if (!array_key_exists($local_dir,$dirs)) { $local_dir = $wkg_dir; }
	if ($local_dir != $dev_dir) { $local_dir = $wkg_dir; }
// Transfer to real program
	include ($local_dir.'/'.$program);
?>
The main program begins by checking whether or not it is running on my home PC (using a
procedure which caused someone here to say 'Yukk'), but which seems to be both logically
sound and reliable.

* 	---------------------------------------------------------------
	1.00  Check if running on home PC & set up defaults
	---------------------------------------------------------------*/
	$home = strtr(getcwd(),"\\","/");  // Ensure Home contains forward slashes
	if (substr($home,0,2) == 'D:') 
		{ 
		$is_local = true; 
		if ($local_dir == 'Dev') 
			{ $util_dir = $local_dir; }
		else { $util_dir = 'D:/Websites/Utilities'; }
		} 
	else 
		{ $is_local = false; $util_dir = 'Utilities'; }

	include ($util_dir.'/Std_utils.php'); 	// Load standard utilities
// Start real work:

If the parameter $is_local is set, I have access to a suite of editing facilities, and
other private areas. These are not present on the public copy, and there is nothing on any
of the menus to suggest that anything is missing.

The parameters $is_local and $local_dir are cached, so they only need to be specified when
the page is first loaded.

I never change the working directory (and it has never yet changed by itself), so I can
load anything in any subdirectory simply by specifying the local path. I also realised
while I was writing this that there is no real reason to have separate Wkg and Utilities
directories (other than that if I only had one version of the program for all local sites,
it would make the result of any bug that made it to the working version potentially that
much more serious).


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux