Sorry, this is OT, but I'm a complete newbie to PHP and I know there are some
smart
PHP coders in this group. If you would indulge me, could you please let me know
the
best way to define paths for includes.
Currently I'm working on a site that is located in a sub-directory of my web
hosting
account. When it's complete, I'll move it to the server of the non-profit group
I'm
doing this for, where presumably it will live at the web root (directly under
the
domain name). Ideally I'd like to have to change some central variable rather
than
have to change paths on every page or in every script.
I found the attached posts in various PHP forums. I'm sure you are familiar with
many of those approaches. Please help my understanding and/or let me know what
is
the best approach to use.
Thanks very much for any assistance, and for understanding and allowing an
occasional OT post.
someone in a forum wrote:
PHP should set its DOCUMENT_ROOT variable to the root folder, so try using the
following form of include statements, and that should work on most servers I
believe.
CODE<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/incHeader.php"; ?>
The DOCUMENT_ROOT variable should automatically always get the right path to
your root html folder on the server, and from there you know the file structure.
someone else wrote:
The first file included in every script is a small config file (in the
same directory as the script itself, so it's guaranteed to be found in a
default PHP setup) which sets the include paths. In that file I have
something like this:
ini_set('include_path', ini_get('include_path') . ':' .
realpath('relative/path/to/include_directory'));
Because of the 'realpath' function, which translates relative paths to
absolute paths, I can copy the directory tree of my application to
another machine and everything will keep working, even if the directory
structure 'above' my application is different.
someone else wrote:
Include "paths.inc" into every file. The file
"paths.inc" contains variables each containing the path to a directory,
relative to the directory that particular "paths.inc" file is in.
For example, I have a paths.inc file in the root directory that has these
variables:
$includesDir = 'includes/';
$cssDir = 'css/';
$jsDir = 'js/';
I also have one in a "photos" subdirectory that has these variables:
$includesDir = '../includes/';
$cssDir = '../css/';
$jsDir = '../js/';
That way the relative paths to several directories can be written to any
file anywhere in my site's hierarchy.
Here are the posts I found via Google:
someone wrote:
Where you left that space for the menu, include this piece of code:
<?php include('menu.php'); ?>
Then save the file. As long as you have the correct path to your menu.php code
this will work once you upload both pages to your PHP compatible server.
I don't suggest using the previous code since it relies on relative paths, and
since you will want to use these page includes across your site you will want
to use absolute paths.
To use absolute paths, use the following code where the first slash indicates
starting from the domain name:
<?php include($DOCUMENT_ROOT . "/incudedfiles/menu.php"); ?>
To be efficient, you should make a folder where all your pages indended for
including will go. Name the folder includes or incl or template or snippets or
whatever you would like.
someone else said:
The path you specify in a PHP include or require statement is a path in the web
server's file system, not a URL. The path is relative to the current page, or
to the 'include_path' variable from php.ini.
If you know the file system path to the directory where your include file is,
you can do this:
Code:
<?php
ini_set('include_path', ini_get('include_path') . $separator . $yourPath);
?>
The $separator is OS-dependent; usually colon on unix/Linux systems and
semi-colon on Windows.
You can find the file system path to your root directory by examining a server
variable:
Code:
<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>
someone else said:
Say your web root is /path/to/webroot/, and you have a site served from
/path/to/webroot/mysite/, you just create a folder below the web root for your
includes, such as /path/to/includes/
In your PHP files, e.g. /path/to/webroot/mysite/index.php, you can include
files from that folder using:
include "/path/to/includes/myinclude.inc.php";
or
include "../../includes/myinclude.inc.php";
Personally Id recommend the former method - using relative paths can quickly
get messy in more complex sites. I would also declare a global variable
containing the include path (or paths), which makes moving to another server or
another location on your server painless.
@Mike: Hopefully that will make it clear. No-one can access myinclude.inc.php
directly, since it resides outside the web root (i.e.
http://mysite.com/index.php will include the file, but
http://mysite.com/includes/myinclude.inc.php doesnt exist).
someone else said:
There is another, easier way to access an include directory outside of the root
directory. In the php.ini file is a section called Paths and Directories. In
there is a parameter called include_path. This sets all of the directories
where the PHP interpreter looks for include files. For instance, if your Web
site root is:
/home/yoursite/htdocs
then create a directory /home/yoursite/includes and place it on the line,
i.e. :
include_path = .:/home/yoursite/includes:/usr/local/lib/php
someone else said:
Why not use a variable that specifies the root of the files, and then use that?
$location = 'path/to/your/files/';
include($location . 'file.php');
One solution is to have a variable in a config file such as
$CONFIG['DOCUMENT_ROOT'] or $CONFIG['APPLICATION_ROOT'] and set the path
explicitly.
You also can't reliably use '../mailer.php' as if it's included from a file in
a different location it will be trying to load that file relative to that
location.
Another way would probably be to set it as a variable (or constant perhaps) in
your script so that it can be changed if necessary to suit the environment.
THESE ONES WERE NOT SENT TO STEVE
phideaux posted a neat trick to make using includes in your code easier.
The idea is you always use one directory for your includes. In your .htaccess
file you add this line
php_value include_path .:/home/user/public_html/includes
Then any time you want to use an include in your file, you dont have to deal
with typing out the big long absolute path (ie. /home/user/public_html/includes
etc etc). All you need do is this:
<?php include(header.php); ?>
Neat trick!
You can have it look in as many directories as youd like as long as you
seperate it with a :. It will look in the order you put things in. If youre
going to use PEAR stuff make sure you include that as well.
Or you could just do something like this
include($_SERVER[DOCUMENT_ROOT]/includes/includefile.php);
Works just as well with not as much work. Just a tip.
Lol, Im phidauex on the boards. Im happy this little hinty made it here
As to noels comment, I find that having to remember to type the entire path,
and environment variable each time is more difficult than just setting the path
one place, and then not worrying about paths at ALL in the actual documents.
The ini_set() function is another handy solution, but remembering to call an
additional function in each page would also negate part of the reason that I
like putting the variable into .htaccess files. That reason being that Im a
terribly forgetful person who wants to do as little work as possible. If I can
set the .htaccess file to include the paths I need, and then just include away
without a care in the world, thats whats best for me!
The ini_set() function would probably be more appropriate for full php
applications, then you could set a default search directory tree for that
specific application using ini_set().
Peace,
sam
Comment from Jester
Time: 2/1/2003, 10:46 am
Yep thats the point portability. Non-apache users cant use .htaccess and
youll find alot of tutorials on the net are geared toward the apache server.
However, youll find that most serious web hosts use apache.
Youll find PHP is full of functions that make it easy for the programmers but
do cause unwanted overhead such as ob_start() which has become popular for
people who get headers already sent errors when setting cookies and starting
sessions. You should really try to structure your code so everything is done in
the right order, as Perl, C/C++ CGI programmers have to. Keep the work your
scripts have to do to a minimum.
you can try this:
<?php include($_SERVER["DOCUMENT_ROOT"]."/includes/nav.php"); ?>
This will save you from specifying the exact path to the document root each
time.
Control your included files
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'));
Some pretty neat stuff going on in here.
First, notice the use of the realpath() function to put the canonicalized
absolute pathname together from a relative link (/../includes/), which by the
way, resides below the public document root ;) -- tidies it up quite nicely.
Second, we concatenate the current server configuration include_path to the end
of our include_path. Why? In case you want to use any existing paths, such as
PEAR classes.
Lastly, notice the use of the PATH_SEPARATOR predefined constant? That way you
can use it on a Windows server or a *nix server without having to modify
anything.
How to Use Include Files in PHP
The PHP configuration file lets you set up an auto_prepend and auto_append
files (header & footer files) which automatically get included on every file.
The only downfall to this is that sometimes you may not want to include a
header and footer file. If you have the auto_prepend and auto_append feature
turned on, and you do not have the appropriate files in the directory you're
trying to view a file in, it will not serve the file out. For example, say you
want to view index.php in www.website.com/temp and the php3.ini file is
configured to auto prepend/append header.php/footer.php and these two files are
missing from the /temp directory, you will get a fatal error when it fails to
open the files it needs to include.
If you use the auto_prepend and auto_append feature, you do not need to add any
additional code to your pages. The PHP engine will automatically add those
files. You can also choose to take advantage of the include_path. You can set
this to your default include directory (say /includes) and when you add the
include tag to your php pages, it will look first in the directory that
particular page resides in to see if there is a file by the name of the include
file and if not, will include the file by that name in the default include path
you've configured.
For example, if you have a default file that you want to include in many of
your files, say a company information file with name, address, phone number,
you can call this "co_info.php" and put it in your default includes directory.
But in the Store directory of your company's website, you want to post
additional or different company info so you created a different "co_info.php"
file and put it in your Store directory. On all your php pages, you'll include
the tag, <? include("co_info.php") ?>. When a visitor comes to that page, PHP
will check to see if there is a "co_info.php" page in its current directory and
if there isn't one, it will include the "co_info.php" from the default include
directory.
The PHP code to include files is: <? include("filename.php") ?>
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
--
squirrelmail-users mailing list
Posting Guidelines:
http://www.squirrelmail.org/wiki/MailingListPostingGuidelines
List Address: [EMAIL PROTECTED]
List Archives:
http://news.gmane.org/thread.php?group=gmane.mail.squirrelmail.user
List Archives: http://sourceforge.net/mailarchive/forum.php?forum_id=2995
List Info: https://lists.sourceforge.net/lists/listinfo/squirrelmail-users