config.php help

Associate
Joined
18 Oct 2002
Posts
1,581
Location
Nottingham
I am in the middle of creating a website with a config.php so that it can be uploaded and changed quickly, the only problem seems to be if the site is uploaded to a folder that isnt the root folder.

I have been using things like server document root commands and things all the way through. I came up with the genius idea of putting in the config the ability to set a folder name if it is uploaded in the folder the only problem is that you cant include the config with out knowing the folder....which is held in the config.

If this makes any sense to you then I am impressed. Basically i need a way of no matter how it is hosted root or not being able to include the config. I dont want to have to include lots of the same code on each page to check this when i can define it in the config. It is just the initial getting to the config.

Any ideas or are you just totally confused!! :confused:
 
Include the config file using relative paths.

So you have:

/index.php
/config/config.php

So in index.php, you have:

Code:
include 'config/config.php';

So long as you keep the app's folder structure you can place that anywhere you like on the server :)
 
this would work for all pages in the top directory of the site.

but if i was in

/otherfolder/example.php

i would have to include the config with '../'

i am looking for a way to have it the same on every page no matter which folder you are in.

EDIT: is there a way of catching the error so that if it cant find the config file i can then tell it to look one directory above. so:

if ( look for config in 'config/config.php' = error)
{
look for config in '../config/config.php'
}
 
Last edited:
If I understand properly, you just want to stick a path into the PHP include_path environment variable. It's basically a list of directories that PHP will look in if it can't find the file in the working one.

If you have access to your server's PHP configs, you can do it in there with any text editor. Otherwise, you can do it at runtime in your script with the line
Code:
ini_set('include_path',ini_get('include_path').':/dir/whatever/whatever:');

where /dir/whatever/whatever is the directory with your config file in. Just stick it in before your include()'s, and you're golden.

But like I say, that's if I understand you properly :)
 
Nim said:
Otherwise, you can do it at runtime in your script
Thing is then you still have to edit alllll the scripts to adjust the folder location :D

I'd say stick the config file at /config/config.php then your app can be anywhere in the filesystem, just use this:

Code:
include $_SERVER['DOCUMENT_ROOT'].'/config/config.php';

Which means throwing the config folder into a fixed place, but then at least your app can be anywhere.
 
Personally I use auto_prepend_file to autoload an initialisation script that sets include paths, as Nim outlined, and other useful initialisations.

For example, in a .htaccess file:
Code:
php_value auto_prepend_file /path/to/init.global.php
Which loads code such as:
Code:
set_include_path(
        get_include_path()
        .PATH_SEPARATOR.'/path/to/conf'
        .PATH_SEPARATOR.'/path/to/somethingelse'
);
With the .htaccess, include paths will be set for the current and all sub-directories, and so you only need to reference files by their name - no need to worry about managing paths. All scripts just include 'config.php'. The file would be loaded if it exists in any of the include paths defined above e.g. /path/to/conf/config.php or /path/to/somethingelse/config.php.

In reality I use a .ini file and a config class - so there are rarely any include() or require() calls - but that's beside the point :D.

Of course, you could just auto_prepend the config file in the .htaccess. No need to edit scripts and your config.php would always be loaded from its location regardless of where the current public PHP script is. That's the simplest and quickest option. Only caveat with this method is that config.php will always load in front of all scripts in your site.
 
Last edited:
Back
Top Bottom