User Contributed Notes: include
garretg@otable.com
23-Feb-1999 11:04
The path for the include command is set by the include_path setting in the PHP3.INI file, which defaults to the current directory.
For a site-wide include that doesn't require settings in PHP3.INI, consider using something like:
<PRE>
require($DOCUMENT_ROOT . "/header.html");
</PRE>
For security reasons, though, be careful about including PHP code this way... it's better to keep it outside the document root directory, where it is only accessible by file sytem commands.
lars@dybdahl.dk
28-Mar-1999 01:29
if file a includes file b, and file b wants to include file c, then include() does not provide any way to include c with a path relative to b, it can only specificy paths relative to a. This is a major obstacle for creating larger projects directly in PHP3.
jja@cmsco.com
09-Apr-1999 01:46
Well... you could always abandon the use of relative paths in favor of explicit paths relative to $DOCUMENT_ROOT.
rmm@dridus.com
10-Jul-1999 08:15
Regarding the security issue noted above, if you want nobody to see the contents of your include files, and you use Apache, you can try stuffing:
<pre>
Order deny,allow
Deny from all
</pre>
into your .htaccess file for where your include file are stored (I use <i>projectdir</i>/include). This also assumes that your Apache setup is configured with <code>AllowOverride all</code> or similar.
This works by virtue of PHP not caring one way or the other about Apache's .htaccess.
mainframe@thewatercooler.com
29-Jul-1999 02:23
If you're using include(), you will probably want to verify the existence of the included file with a file_exists() first, so that if by mistake the file doesn't exist, evil users won't see your directory structures.
I use this procedure in a set of php3's that provide the authentication for my site, where you call the php3 with a given filename, that is displayed only if the user has been authenticated first.
fran0382@tc.umn.edu
24-Aug-1999 02:37
You can use fopen() to include files from a web or ftp site like this:
fopen("http://www.somesite.edu/foo.html", "r"); If you do that though, make sure you include error checking to see if the file was actually opened or not.
08-Sep-1999 10:37
you can do this <pre>
<?
$scriptname = "dillwack.php3";
$args = "?id=2332&hits=233";
$path = $scriptname.$args;
include($path); /* or */ require($path); ?></pre>
It worked on <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2Fja%2F%26quot%3Bhttp%3A%2Fwww.dtheatre.com%26quot%3B%26gt%3Bdtheatre.com%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>
mhobson@pacbell.net
08-Nov-1999 03:46
Re: Passing arguments to included scripts--
If you are using Apache mod_php, try "virtual($url-with-parameters)";
You can make the url be a script on your site or one
on some other site.
Docs for this are in the Apache section of the function reference.
mhobson@pacbell.net
08-Nov-1999 03:57
<B>include</B> loads and runs a PHP3 Script
<B>But</B> the included script will <B>not</B>
receive <I>any</I> CGI variables unless you
make the <I>web server</I> load and run it by
calling <B>include</B> with a URL instead of
the local file name.
Instead of:
<PRE>
include("myscript.inc?arg=val");
</PRE>
use:
<PRE>
include("http://my.server.com/cgi-bin/myscript.php3?arg=val");
</PRE>
or whatever URL would make that work on your server. This is an
alternate solution for those who aren't using Apache mod_php3.
pdt@pdt.cx
27-Nov-1999 02:38
I ran across multiple inclusion problems with a recent project.. I had one file global.php3 that was included in several places so I added a $GLOBALINCLUDED variable in the file and anywhere I included the global file i did this:
<PRE>if(!isset($GLOBALINCLUDED) {
include "global.php3";
}
vksgeneric@hotmail.com
08-Dec-1999 01:27
probably better than the suggestion above is a thing similar to the #ifndef...#define...#endif in the c/c++ header files. Put this at the beginning of the file included:
if(isset($GLOBAL_INC)){
return;
}
$GLOBAL_INC = "set";
and remember to use include() rather than require() to include that file.
digita1l@hotmail.com
25-Dec-1999 12:32
a major security risk is leaving *.inc files world readable. In apache you can add the following to your httpd.conf file to deny access to *.inc:
<Files ~ ".inc">
Order allow,deny
Deny from all
</Files>
nate@nojunk_allmed.net
13-Jan-2000 07:01
I also ran into the problem that many others reported about multiple includes.
I liked the idea the person had about useing something simular to c's #ifdef and #def, but when I tried his example it didn't work becouse I was including the file inside of a function the first time, which made the varible local. Then when I tried to include it again it didn't find the previous local varible.
The solution I came up with was to use constants like so:
if(GETSETTINGS_INCLUDED==1){
return;
}
define("GETSETTINGS_INCLUDED", 1);
Since constants are always global, this works great. Of course the fact that constants are global might not be such a great idea, I can see where it would be handy to have private constants in a class definition. But this will work.
btol45@calvin.edu
12-Feb-2000 04:07
One of the problems I've struggled with for a long time was sending a header *after* I included a file:
<PRE>
include("somthing.php");
header("Location: something_else.php");
</PRE>
This never worked for me, but it does work if you
wrap the include in an if statement:
<PRE>
if (include("something.php")) {}
header("Location: something_else.php");
</PRE>
Now you can include the file AND redirect.
milan@insource.nl
25-Feb-2000 11:07
I ran into a security issue where web users were able to just download the .inc files and while doing that, their sourcecode with ofcourse passwords... My solution for this problem (without having to create .htaccess files) is adding the .inc extension to "AddType application/x-httpd-php3 .php3" in the appache httpd.conf file. Or if you can't edit the httpd.conf: just give your include files the same extension as your normal php files.
jengo@NOSPAMmail.com
17-May-2000 11:05
Just a quick note on something that drove me nuts for about an hour last night. I was tring to included a common footer into my program and its was completly refussing to add the file in there. It keep on saying it doesn't exists.
Finally after an hour of 4 letter words I figured out that you need to chmod the included files to 755.
This way someone else does go through the hell I did :)
simon.li@hongkong.com
02-Jul-2000 07:18
It sound a little stupid. But it is another remind.
If you are using a maching which run windows + PHP Win32, and finally going to put all your work (htm, php3, etc) to a unix server, then you must be carefully not to mistype your filenames.
Coz in windows file.php and FILE.php are the same thing, but not in unix. If you mistyped, then some of your code works and some doesn't and it will drive you crazy.
f97-ali@f.kth.se
24-Jul-2000 04:19
php3 will happily include the same file multiple times, which is usually a bad idea. This is why you should add C-style inclusion-guards in the beginning of inc-files.
If you have an inc-file called login.inc, you should do something like this in the beginning:
<PRE>
if defined( "LOGIN_INC" )
return;
define( "LOGIN_INC", TRUE );
#code goes here...
...
</PRE>
This way your inc will only be included once.
breuerm@execpc.com
24-Jul-2000 05:00
Because require statements are replaced with the contents of the included file, the following construct does not work:
if (defined("MYSOURCE_PHP")) { return; } define("MYSOURCE_PHP",1);
If the file is being required for the second time, the calling script returns at this point, and no further code is executed. To get around this, I used:
if (!defined("MYSOURCE_PHP)) { define("MYSOURCE_PHP",1); /* function definitions */ }
This seems to resolve the issue for me using 4.0.1pl2.
oystein@edge.no
11-Aug-2000 08:05
To prevent users from being able to view your include files with passwords and/ or other sensitive data, you should always add a .php(3) extension, e.g.:
include("header.inc.php");
include("config.inc.php");
This way you won't need to specifically hide your include files with, for instance, Apache directives.