[BlueOnyx:11890] Re: PHP include files

Jim Matysek matysekj at usms.org
Thu Jan 3 10:04:47 -05 2013


Here's how we handle includes. The issue is that you'd like to include 
something like a footer file using the same line of code on any page, 
even though your pages are in different sub-directories. So you don't 
want to have to include footer.php on one page and ../footer.php on 
another page.

Create a /home/includes directory and add it to your PHP include path 
(we added it to the php.ini file). Create a file 
/home/includes/sitedefs.php:

<?php
$sitenum = preg_split('/\//',$_SERVER['SCRIPT_FILENAME'], 6);
if ($sitenum[2] == "sites")
{
   $sitenumber = $sitenum[3];
   $site = "/home/sites/$sitenumber";
}
elseif ($sitenum[2] == ".sites")
{
   $siteprefix = $sitenum[3];
   $sitenumber = $sitenum[4];
   $site = "/home/.sites/$siteprefix/$sitenumber";
}
elseif (preg_match('/^[a-z,A-z]\:/', $sitenum[0]))
{
   $site = substr($sitenum[0], 0, 1) . ":\www.mysite.com";
}
else
{
   $site = "/home/sites/www.mysite.com";
}
?>

Now sitedefs.php is available for inclusion from anywhere on any site on 
the server and defines the path to the root of the site you are 
currently working in. So from within any PHP file on any site, you can do:

<?php
include_once "sitedefs.php";
include "$site/web/footer.php;
?>

Note that if you put your includes out of the web directory, this is 
handled also, just include $site/includes/footer.php instead.

The last elseif clause in the sitedefs file allows us to run our sites 
using XAMPP on our PC's also, from a c:\www.mysite.com directory that is 
an exact copy of the file structure from the BX machine.

Note that doing this also allows us to have exact copies of code in a 
test.mysite.com site on the server. All includes in our scripts are 
relative to the $site variable, so everything just works.

-jim


> On Thu January 3 2013 03:32, Steven Howes wrote:
>> On 2 Jan 2013, at 17:50, Larry Smith wrote:
>>> His index.php file has:
>>>> <?php include '/includes/footer.php'; ?>
>> Surely it should be 'includes/footer.php' unless you let customers upload
>> to the root of your filesystem?... Remove the leading slash and I suspect
>> it will work.
>>
> Tried that, also tried turning on Allow URL open and Allow URL include
> in the PHP GUI and changing it to the site full URL (for their local includes
> directory) but still get errors.  I had presumed the leading slash pointed
> to their web space, but tried dot slash with the includes being in the same
> directory and still does not work.
>





More information about the Blueonyx mailing list