My PHP application has different config files with their different settings and their different paths. But all of them need to access to just one database connection file. I would like to avoid mention its path separately on them with many ../
and ../../
. How can I write a same code and use it in all of them?
Imagine that my PHP application has this hierarchy for its config files:
root
root/config/db.php (This file is what I want to mention from all config files.)
root/admin/config/conf.php
root/users/conf.php
Actually what I need is something that detect the root path of my project and create a same directory path generally.
Use $_SERVER['DOCUMENT_ROOT']
and then path to your file.
Use PHP's class autoloading, the catch being you then need to wrap up your config information in a class, but it works.
require_once("../../config/db.php");
Include this file into "conf.php" file
Set the include_path
directive as either root/
or root/config/
either by editing your php.ini file or by calling set_include_path
on every page. This way, you can just include/require "db.php"
on each page.