views:

419

answers:

5

Do all php include files have to be in an include directory, or is that just an organizational convenience? Can I include files from any directory using the path to that directory? Do html files that contain php includes have to have a php extension? If so, I guess that would mean all of my html docs would be php if they all have the menus I am planning to "include". Is that acceptable protocol to have every file on your site be a php file?

A bow in humble reverence...

p.s. is there a good bible on this topic!?

+7  A: 

You don't need to have your php includes anywhere particular, or have any file extension...

Calling include('/path/to/your/file/with/any/extension.extension'); should include it as a php file

Normally safer to call files with php code in with a php extension tho.

Mark Milford
+1  A: 

If you put PHP in a .html file and include() it, it should work. However if someone requests the .html file directly from the server they could be able to see all your PHP code un-processed which is dangerous. Therefore if you are doing this then either put these files in a non-web-accessible directory (e.g. above the document root) or change your web server configuration so that .html files are treated as PHP.

You should be able to include() any file but you may get problems accessing some directories if there is an open_basedir restriction in PHP (which you will probably find on most webhosts)

Tom Haigh
+1  A: 

You can include files from anywhere on the server (depending on file access privileges and security restrictions, of course).

Using an includes directory helps keep things well organized, but is not required.

If a file contains PHP code, it should be saved as a .php file - otherwise if it is called directly the code will not run. This leaves you with a file which behaves differently depending on how it is accessed, which is probably not what you wanted and could be a security threat.

To get around this, I have seen some people override file-types so the server treats all .html files as PHP scripts - I do not recommend this, as it will slow processing of all files even if they do not include any PHP. Follow the Principle of Least Surprise - let your HTML files be .html and your PHP files be .php - and everyone will be happier.

Hugh Bothwell
A: 

Hugh Bothwell said, "let your HTML files be .html and your PHP files be .php - and everyone will be happier."

If we want to call a php file from inside an html file using an include statement, do I have to change the html extension to .php for it to work?

James Musser
A: 

James Musser:

I think you are referring to Apache SSI includes now (see http://httpd.apache.org/docs/2.2/howto/ssi.html#standard-footer)

Not knowing the answer, I tried it, and here are my results

Extension  Call method        PHP runs
 .html      browser            no
 .html      Apache #include    no
 .html      PHP include()      yes
 .php       browser            yes
 .php       Apache #include    yes
 .php       PHP include()      yes

(Testing done with WinXP, Apache 2.2.3, PHP 5.2.26 and CentOS 5.2, Apache 2.2.3, PHP 5.1.16)

So your answer is YES - if you wish to call a PHP script through an Apache SSI #include, the script must have a .php extension (or some other extension, ie .php4 .php5 etc, which is registered with Apache as being a PHP script), otherwise it will NOT run.

Hugh Bothwell