6

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

Exploit
  • 6,278
  • 19
  • 70
  • 103

4 Answers4

4

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.

mario
  • 144,265
  • 20
  • 237
  • 291
  • can you write how i would do it in htaccess? im sorry i lack some knowledge in htaccess , thanks – Exploit Oct 22 '11 at 22:36
2

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.

majic bunnie
  • 1,395
  • 2
  • 10
  • 21
0

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

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • it is already defined as a constant in the config.php page. but in order to include that config file on every page i cant use the constant. the config file has to somehow to defined in apache but i dont know how. i cant use the constant on pages that dont have the config file on it otherwise its gonna trow errors. – Exploit Oct 22 '11 at 22:31
0

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

John Watson
  • 2,554
  • 1
  • 17
  • 13