Managing includes


D

dorayme

For almost all of the sites I maintain, I am decking them out
with PHP includes, some are operating fine outside on external
servers. There is no management problem for this majority because
of the portability of the construction I am using:

($_SERVER['DOCUMENT_ROOT'].'/site/includes/section.inc'); ?>

Works beaut on both external and my local apache Mac server.

But, now that I have the solution to a global construction for a
Windows server for a couple of my sites (courtesy of help here in
a recent thread), I am going to have a slight management problem.
Because the construction used for these servers is double dutch
to my Mac server.

<? include "e:\inetpub\abc12345\includes\section.inc"; ?>

my solution so far is to put both constructions of the includes
on the files with one of them commented out for use here on my
server (for testing) and the other commented out for upload to
the actual server delivering the company site to the internet.
This is obviously easy to do for just a few files. And for the
business of uploading the newly "decked out with includes" whole
site, I guess I can just use S&R (on the whole site) to switch it
back and forth.

Anyone got any better idea of how to do this? It's surely not
that bad what I propose but I have this instinct that there are
likely smarter way of proceeding.
 
Ad

Advertisements

N

Neredbojias

For almost all of the sites I maintain, I am decking them out
with PHP includes, some are operating fine outside on external
servers. There is no management problem for this majority because
of the portability of the construction I am using:

($_SERVER['DOCUMENT_ROOT'].'/site/includes/section.inc'); ?>

Works beaut on both external and my local apache Mac server.

But, now that I have the solution to a global construction for a
Windows server for a couple of my sites (courtesy of help here in
a recent thread), I am going to have a slight management problem.
Because the construction used for these servers is double dutch
to my Mac server.

<? include "e:\inetpub\abc12345\includes\section.inc"; ?>

my solution so far is to put both constructions of the includes
on the files with one of them commented out for use here on my
server (for testing) and the other commented out for upload to
the actual server delivering the company site to the internet.
This is obviously easy to do for just a few files. And for the
business of uploading the newly "decked out with includes" whole
site, I guess I can just use S&R (on the whole site) to switch it
back and forth.

Anyone got any better idea of how to do this? It's surely not
that bad what I propose but I have this instinct that there are
likely smarter way of proceeding.

How about a simple conditional checking for the existence of "e:\inetpub
\abc12345\includes\section.inc"?

--
Neredbojias

Once I had a little bird
That made me rather hasty.
So now I have no little bird,
But it was very tasty.
 
N

Neredbojias

How about it... I'll nick out to see if I have one in the boot of
my car and if I find one I will.. I will... well, I will bring
it inside to keep me company I guess. My cat died last week.

Well, I don't have a mac to check, but this might help further:

http://us2.php.net/file_exists

--
Neredbojias

Once I had a little bird
That made me rather hasty.
So now I have no little bird,
But it was very tasty.
 
D

dorayme

Disco Octopus said:
dorayme wrote: ....

In your php.ini file, or you .htaccess file, or your httpd.conf file,
you have a include setting...

This would be unique to the server. So you only really need to enter
your include like this....

<? include "section.inc"; ?>

You should look into the "include_path" setting in php.

*or*, you could do something like this.... (but then, you may need to
include this chunk of code into its own included thing???)


<?php
$GLocalDev1 = 'l-mylocallinuxserver.com';
$GLocalDev2 = 'l-mylocalmacserver.com';
$GLocalDev3 = 'l-mylocalpcserver.com';
$GLive = 'live-site.example.com.au';
if ($_SERVER['HTTP_HOST'] == $GLocalDev1){
$IncludePath = "e:/inetpub/abc12345/includes/";
}
elseif ($_SERVER['HTTP_HOST'] == $GLocalDev2){
$IncludePath = "/home/www/mywebsite/includes/";
}
elseif ($_SERVER['HTTP_HOST'] == $GLocalDev3){
$IncludePath = "/whatever/your/mac/path/is/includes/";
}
include $IncludePath . "section.inc";
?>

Thanks Disco Octopus, will look at it tomorrow... one thing is
this, I can fiddle about as I please with my own server but have
little control over a public Windows server and am looking to an
avenue that does not involve altering that server if possible. I
ftp things up and that causes the least trouble, no awkward
communications needed with giant ISP concerned. My plan (snipped
above) has that going for it (even if it is a bit inelegant).
 
Ad

Advertisements

T

Toby A Inkster

Disco said:
You should look into the "include_path" setting in php.

include_path is awesome indeed. You create a directory, put all your
included files in there and then set the include_path for your website
to point to that directory. (This can be set in .htaccess if need be.)

Then in your code, you just reference:

include "something.php";

and the PHP interpreter will look to see if there is a file called
"something.php" in your include_path. Whatsmore, your include_path can
contain multiple paths, and your PHP script can modify it on-the-fly.

For example, my CMS has this at the top of its index.php file:

$ip = get_include_path();
$dirs = array('includes', 'lib', 'PEAR');
$base = dirname(__FILE__);
if (!preg_match('#/$#', $base)) $base .= '/';
foreach ($dirs as $d)
$ip .= ':' . $base . $d . '/';
set_include_path($ip);

define('DEMIBLOG_BASE_DIR', $base);
define('DEMIBLOG_SETTINGS_DIR', $base);

This takes the path of the directory where index.php is itself contained,
and calls that the "base" directory. It then finds subdirectories of the
base directory called "includes", "lib" and "PEAR" and adds them to the
include_path. It then saves the base directory as a PHP constant, and
defines a "settings directory" which is the same as the base directory by
default. (All other important directories are then loaded up as settings.)

Overall, "index.php" is lean and mean: only 17 lines excluding whitespace
and comments -- so I'm sure the fact that it dedicates over half of its
lines to playing around with paths says something very profound, though
I'm not entirely sure what.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 6 days, 15:07.]

The End of an Era
http://tobyinkster.co.uk/blog/2007/06/26/end-of-an-era/
 
J

Jonathan N. Little

dorayme said:
For almost all of the sites I maintain, I am decking them out
with PHP includes, some are operating fine outside on external
servers. There is no management problem for this majority because
of the portability of the construction I am using:

($_SERVER['DOCUMENT_ROOT'].'/site/includes/section.inc'); ?>

Works beaut on both external and my local apache Mac server.

But, now that I have the solution to a global construction for a
Windows server for a couple of my sites (courtesy of help here in
a recent thread), I am going to have a slight management problem.
Because the construction used for these servers is double dutch
to my Mac server.

<? include "e:\inetpub\abc12345\includes\section.inc"; ?>

my solution so far is to put both constructions of the includes
on the files with one of them commented out for use here on my
server (for testing) and the other commented out for upload to
the actual server delivering the company site to the internet.
This is obviously easy to do for just a few files. And for the
business of uploading the newly "decked out with includes" whole
site, I guess I can just use S&R (on the whole site) to switch it
back and forth.

Anyone got any better idea of how to do this? It's surely not
that bad what I propose but I have this instinct that there are
likely smarter way of proceeding.

What I do is have a DNS server setup for my LAN and set to a private TLD
so all websites have a different domain name whether it is the "live"
version or the locally maintained mirror, e.g.,

live => www.example.com
local => www.example.private.lan

then I have a universal configuration object which in addition to
maintaining client information also configs URL and paths...

in the script I define the local and live server names for each site

define('LOCAL_SERVER', 'www.example.private.lan');
define('REMOTE_SERVER', 'www.example.com');

and upon init of the object a method sets a flag for whether or not the
site is "on web" or not.

....
$this->onweb=($host==REMOTE_SERVER)? 1 : 0;
....

the flag is used to set any differences between local and live server
condition like SSL url and server root, include paths, mailserver
settings, etc.
 
N

Neredbojias

17 years old. Dear little thing.

I've noticed, in general, women seem to prefer cats, and men, dogs. Do you
think it says anything pertinent about the human psyche or just that men
are drawn to a wagging tail?

--
Neredbojias

Once I had a little bird
That made me rather hasty.
So now I have no little bird,
But it was very tasty.
 
N

Neredbojias

include_path is awesome indeed. You create a directory, put all your
included files in there and then set the include_path for your website
to point to that directory. (This can be set in .htaccess if need be.)

Then in your code, you just reference:

include "something.php";

I see no advantage to this. What is wrong with just inserting the path
as you do in a link, eg: ../par_dir/somefile.html? That way one could
address an include file anywhere on the site with a minimum of confusion
or effort. Now I realize the powers that be haven't allowed for this,
but stating an include_path is awesome seems a bit much.
and the PHP interpreter will look to see if there is a file called
"something.php" in your include_path. Whatsmore, your include_path can
contain multiple paths, and your PHP script can modify it on-the-fly.

For example, my CMS has this at the top of its index.php file:

$ip = get_include_path();
$dirs = array('includes', 'lib', 'PEAR');
$base = dirname(__FILE__);
if (!preg_match('#/$#', $base)) $base .= '/';
foreach ($dirs as $d)
$ip .= ':' . $base . $d . '/';
set_include_path($ip);

define('DEMIBLOG_BASE_DIR', $base);
define('DEMIBLOG_SETTINGS_DIR', $base);

This takes the path of the directory where index.php is itself
contained, and calls that the "base" directory. It then finds
subdirectories of the base directory called "includes", "lib" and
"PEAR" and adds them to the include_path. It then saves the base
directory as a PHP constant, and defines a "settings directory" which
is the same as the base directory by default. (All other important
directories are then loaded up as settings.)

Overall, "index.php" is lean and mean: only 17 lines excluding
whitespace and comments -- so I'm sure the fact that it dedicates over
half of its lines to playing around with paths says something very
profound, though I'm not entirely sure what.

Clever but cumbersome. And how many pagemakers are as adept at php as
you?

--
Neredbojias

Once I had a little bird
That made me rather hasty.
So now I have no little bird,
But it was very tasty.
 
Ad

Advertisements

D

dorayme

Neredbojias said:
I've noticed, in general, women seem to prefer cats, and men, dogs. Do you
think it says anything pertinent about the human psyche or just that men
are drawn to a wagging tail?

I have walked a dog on the foreshores of Botany Bay for years,
and have keenly noticed women walking dogs with tails. I have
seen no women walking cats, with or without tails.
 
D

dorayme

"Jonathan N. Little said:
dorayme wrote:
What I do is have a DNS server setup for my LAN and set to a private TLD
so all websites have a different domain name whether it is the "live"
version or the locally maintained mirror, e.g.,

live => www.example.com
local => www.example.private.lan

then I have a universal configuration object which in addition to
maintaining client information also configs URL and paths...

in the script I define the local and live server names for each site

define('LOCAL_SERVER', 'www.example.private.lan');
define('REMOTE_SERVER', 'www.example.com');

and upon init of the object a method sets a flag for whether or not the
site is "on web" or not.

...
$this->onweb=($host==REMOTE_SERVER)? 1 : 0;
...

the flag is used to set any differences between local and live server
condition like SSL url and server root, include paths, mailserver
settings, etc.

I am impressed. Do you maintain any sites on external Windows
servers?
 
D

dorayme

Toby A Inkster said:
Whatsmore, your include_path can
contain multiple paths, and your PHP script can modify it on-the-fly.

This is the sort of thing that is getting my attention. That it
can be done is encouragement. I proceed slowly, fathoming what is
said and testing for my own situation.

Do you maintain any sites on external Windows servers?
 
J

Jonathan N. Little

dorayme said:
I am impressed. Do you maintain any sites on external Windows
servers?

Nope, I try to avoid IIS like the plaque. Our public library use it and
their PAC (Public Access Catalog) is in ASP...other then keep their
network and server running I let their software fiddle with the site.
 
D

dorayme

"Jonathan N. Little said:
Nope, I try to avoid IIS like the plaque.

hmmm... mostly for me, the prob became pressing with having to
work with such a server. It's all very manageable... but your
set up is neat.
 
Ad

Advertisements

N

Neredbojias

I have walked a dog on the foreshores of Botany Bay for years,
and have keenly noticed women walking dogs with tails. I have
seen no women walking cats, with or without tails.

Botany Bay...

Reminds me of a song I sung lo-o-o-o-o-ng ago and the part I (may
imperfectly) remember.

It's not leaving ol' England we care about
Or sailing for shores far away.
It's the bloomin' monotony wears us out,
And the prospect of Botony Bay.

Never knew it was in Sydney.

--
Neredbojias

Once I had a little bird
That made me rather hasty.
So now I have no little bird,
But it was very tasty.
 
T

Toby A Inkster

Neredbojias said:
I see no advantage to this. What is wrong with just inserting the path
as you do in a link, eg: ../par_dir/somefile.html?

Firstly, consistency. If I want to include "navigation.php" from
"/index.php", with your system you have to write:

include "includes/navigation.php";

but if you want to include the same navigation menu from
"/birds/parrots.php" then you need to use:

include "../includes/navigation.php";

The path is different, you see. Now, say my site takes on more of a parrot
focus, so I decide to move "parrots.php" out of the "birds" directory and
into my site's root directory, then this breaks the include path, and I
need to change it.

Using include_path makes maintenance a lot easier. I put "navigation.php"
into a directory listed in my include path, then I can use it on
"/index.php" or "/birds/parrots.php" like this:

include "navigation.php";

without having to specify the path to it. I can then move my "parrots.php"
file about as much as I like, and don't need to make any changes to it.

This is much the same way that C programmers work. They specify the file
name of a library they want to include, but not the full or relative path
to it. They rely on the build environment to specify the location of all
the library files.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 7 days, 11:27.]

Long-Awaited Zeldman Article
http://tobyinkster.co.uk/blog/2007/06/27/zeldman-in-time/
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top