Forum Moderators: coopster

Message Too Old, No Replies

Accessing PHP includes folder in root dir from 2+ levels deep

Includes folder is password-protected

         

dougmcc1

4:44 am on Nov 4, 2003 (gmt 0)

10+ Year Member



I'm getting ready to publish a directory and I noticed my includes don't work if I request a page more than 1 level deep in the site heirarchy. The include folder is password protected and I have tried setting permissions to 777. I get the following two error messages for each include line on pages 2 levels deep or more:

Warning: main(http*//www.site.com/includes/include_name.php): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /home/***/public_html/category/pagename.htm on line 1

Warning: main(): Failed opening 'http*//www.site.com/includes/include_name.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/***/public_html/category/pagename.htm on line 1

So basically my question is how can I let pages 2+ levels deep access the includes folder in the root directory with absolute links to http*//www.site.com/includes/page_include.php while not letting in visitors to the site? I don't want people to be browsing my includes folder.

Thanks.

jatar_k

5:00 am on Nov 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



to prevent people being able to browse that folder you could use an index file that just redirects them to the homepage instead of user/pass.

Then you should be able to just
include $_SERVER['DOCUMENT_ROOT'] . "/includes/include_name.php";

or you could get your host to turn off the listing dir contents in apache.

NickCoons

6:24 am on Nov 4, 2003 (gmt 0)

10+ Year Member



dougmcc1,

I usually reference my include files with an absolute filesystem path, like:

include("/home/directory/includes/included-file.php");

...instead of using an http:// path. And then the directory that the includes are in are not accessible. Meaning, there is no http:// path that points to /home/directory/includes... it is outside of ./public_html.

coopster

11:00 am on Nov 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Here is another nifty little trick:

ini_set('include_path', ini_get('include_path') . ':' . $_SERVER['DOCUMENT_ROOT'] . '/../includes/');

It takes a look at the current include path and appends your non-public (below the 'www' root) include path to the end of it until your script completes. I wanted a solution that required no modification/configuration of directive files. Slick.

Your include path will end up looking something like this:


.:/usr/local/lib/php:/my/server/web/path/mysite/../includes/

[edited by: coopster at 12:42 am (utc) on June 29, 2005]

dougmcc1

11:31 pm on Nov 4, 2003 (gmt 0)

10+ Year Member



It works. Thanks for all the suggestions guys.