Accessing config files outside of web directory

software development

#1

I know I can place some files outside of my web accessable directory structure in order to keep them further from prying eyes. But for some reason, I seem to be unable to get to the file with an include or require command in php. I suspect I’m using the wrong path, but can’t seem to find the right information in the wiki. Can anyone point me to the correct place. Also, what is the minimum rights I can set on this directory to make it readable only by me and my web site(s).

I created a directory off the root of my user space here on DreamHost with a file for holding constant definitions used by my site.

/blah/somecostants.php

What path can I use in my require_once to access this location?

require_once(’/blah/someconstants.php’) does not work.

Any help is greatly appreciated including pointing me to the correct locations in the wiki, as I’m sure this is covered there somwhere.


http://www.TheHelpfulNeighbor.com


#2

You’d need to do something like below in order to get the webserver to “see” the external environment:

That should work with your require once function.
Though keep in mind, anything else within the script (below the ‘chdir’ line) will now attempt to access files starting in your root directory (or wherever your set their access).
I personally don’t consider this to be “safe”, so I would suggest making whatever script that requires this to be not publicaly accessible. Either that, or create a folder within your root directory specifically for your constants definitions (ie. /home/user/constants_dir).


#3

If your web page is in the web document root and you place your required file below that in the root then does

require_once(’…/someconstants.php’)

work?

Or if you are in a folder in your web document root (example.com/folder) then does

require_once(’…/…/someconstants.php’)

work?


Norm

Opinions are my own views, not DreamHosts’.
I am NOT a DreamHost employee OK!! :@

You act on my advice at your own risk!


#4

In Reply to:

"If your web page is in the web document root and you place your required file below that in the root then does

require_once(’…/someconstants.php’)

work?"

This works. My only concern now is pages in folders other than the root locating it. But I’ll work that out. Thanks for the suggestion.


http://www.TheHelpfulNeighbor.com


#5

‘…’ means folder above this one.

use …/…/etc/… to navigate through and find the folder you want.

For example:

I have a file, it’s located in http://website/users/abyss

I want it to include a file from http://website/ - In this case, you include …/…/filename

Say, you’re in /users/abyss, and want a file in /users/foo
Do: include …/foo/filename .

Do these relative to the location of the file you have the include statement in.

I tried to explain it best as I can, Understand? xD

-vI-


#6

In response to
"
’…’ means folder above this one.

use …/…/etc/… to navigate through and find the folder you want.

For example:

I have a file, it’s located in http://website/users/abyss

I want it to include a file from http://website/ - In this case, you include …/…/filename

Say, you’re in /users/abyss, and want a file in /users/foo
Do: include …/foo/filename .

Do these relative to the location of the file you have the include statement in.

I tried to explain it best as I can, Understand? xD"

Yes Abyss,
Thanks. I do understand the …/ traversing up a level in the directory tree. My issue was with how to handle it for files in different levels of my site. Since I use the same include files to do basic functionality where needed from all pages in my site that need them, files they themselves may need to include are in different relative paths to the page being loaded. And unfortunately, php looks for files relative to the main script rather than the script looking for the file.

So if I have a structure like…

/www
/www/includes
/www/admin

and pages in /www and pages in /www/admin load the same file out of the /www/includes folder to then relatives paths inside those files are different.

I had handled this before by placing a locals.php file in each folder, and it defines settings required by pages in that particular folder. Among them is $RootPath which contains the relative path to the root of my site.

So locals for /www has $RootPath = ‘’;
For /www/admin $RootPath = ‘…/’ ;

Then in my include or require statements I always use the $RootPath variable if I need to get down to my main includes or whatever other directories.

require_once($RootPath . ‘includes/someincludesfile.php’);

It works just fine in this case as well. I don’t know why using relative paths didn’t cross my mind for getting down below my web directory as well.

require_once($RootPath . ‘…/blah/someglobals.php’);
gets me my globals file just fine as well, and since I already have $RootPath defined and available to all my pages, I’m good to go.

Thanks everyone for the suggestions.


http://www.TheHelpfulNeighbor.com