Exploit
Exploit

Reputation: 6386

when placeing a config file outside the public root. how do i call it on every page without defining the path

this is how my file structure is. [..] are folders

[public]
[libs]
[crontab]
[logs]
[sessions]
[tmp]

config.php

the domain is set to the public folder as its root. everything else including the config.php file is outside the root. the config contains db connection information and other necessary settings.

every page in the public folder i have to put the entire path so include the config file. problem is when i move everything from my local machine to the public server i have to go through every page and change the path included for the config file. is there a better way? maybe settings a path in php.ini or something like that?

thanks

Upvotes: 6

Views: 4747

Answers (4)

John Watson
John Watson

Reputation: 2583

See set_include_path() and the docs for the include_path ini setting.

Upvotes: 0

majic bunnie
majic bunnie

Reputation: 1405

A technique that I frequently use to include files relative to a particular location in your webroot structures is to use an include/require like this:

require_once(dirname(__FILE__) . '/path/to/include.php');

This way, codebase migrations don't need to change any hard-links or constants so long as the files remain relative to each other in the same way.

Upvotes: 2

mario
mario

Reputation: 145482

In that specific case you can use:

include("$_SERVER[DOCUMENT_ROOT]/../config.php");
# Note: special exception array syntax within double quotes.

Which resolves to the public directory first, but moves one level back ../ to reach the config file outside of the docroot.


But you could also use SetEnv in your .htaccess to define another $_SERVER environment variable for the occasion.

Upvotes: 4

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

You could just use constant for your path and change it once, and define that constant on every path - included file

You can also take a look at include path http://php.net/manual/en/function.set-include-path.php

Upvotes: 0

Related Questions