tags:

views:

174

answers:

2

Ok, I have seen a lot of gripes about the php include() directive here. It seems this is become a cause of grief for me too.

My site structure is something like this:

public_html\index.php
public_html\includes\content.inc.php
public_html\language\en\language.inc.php
public_html\classes\db.inc.php

The site runs swell on Apache no issues. It does not work on IIS however (grr... #@!!!%).

Let me explain: What is done is that index.php is always called by the webbrowser and depending on the request, it includes content.inc.php (there are many of these content files and index.php includes the appropriate one based on logic)

// -- index.php --
include(includes/content.inc.php);

So far it works well on both Apache and IIS.

Now content.inc.php in turn includes lang.inc.php and db.inc.php.

It looks like:

// -- content.inc.php --
include(language/en/language.inc.php)
include(classes/db.inc.php)

These lines work well on Apache but IIS complains it cant file the files to be included.

Why? Because Apache maintains the current folder "." is public_html\ where the script execution first started. IIS on the other hand changes the meaning of "." to be the file that is currently being processed. i.e after including content.inc.php, IIS interprets "." as being public_html\includes\

Now I know one solution is to change all include paths to be absolute in some way. But my question is, is this really an Apache/IIS issue as I understand it? Is there an IIS or PHP setting to make it behave correctly without mucking about in the code?

BTW, PHP.ini has the

include_path=".;C:\PHP\PEAR"
+2  A: 

Your include files should be OUTSIDE of your public_html tree completely.

// Your setup should resemble:

/public_html/index.php
/includes/
/include/languages
/include/classes

// If on your phsical drives, they equate to;

c:/wwwroot/public_html/index.php
c:/wwwroot/includes/
c:/wwwroot/includes/languages
c:/wwwroot/includes/classes

Then the setting in your PHP.INI file should be changed to:

include_path = ".;C\PHP\PEAR;C:\wwwroot\includes"

Where I am guessing something like wwwroot is the directory you put your html in, change it as necessary.

Restart the IIS service to pick up the new ini setting.

echo ini_get('include_path') ; 

to keep your sanity...

Now PHP will always look inside wwwroot/includes for your include files, so you can include things like /languages by doing this;

include 'language/eng/language.inc.php' ;

Which is what you want.

Cups
Thanks. I was thinking this would be the solution. However I don't get why the include files should be outside of public_html. I believe this is for security, to prevent someone from invoking them over the web?
Ali
Because if I knew the address of your website, I could just access:public_html\classes\db.inc.php by typing yoursite.com/classes/db.inc.php - which is not what you'd want at all.
Cups
A: 

I typically like to include from the root of the server such as include '/includes/language.inc.php'. This way, it will work from any folder within your site structure. I link all my CSS and JavaScript this way as well, just makes it easier to put stuff in include files when you have files from different folder structures calling them.

Shawn Steward
But that would only work if all PHP/javascript/CSS folders were placed at the root of your domain. If your entire PHP app was in a subfolder of the root it would mess things up.
Ali
Ok then... /phpapp/includes/whatever...
Shawn Steward