webmasters
webmasters

Reputation: 5841

Working with paths - make a SITE_ROOT constant available on each page

I am a php newb and I am struggling with paths.

I have a config.php file in the root of my application where I have defined a constant which stores the parent's directory path.

define("SITE_ROOT", realpath(dirname(__FILE__)));

The site will have different folders: /includes/ /admin/ /theme/

Now, even if the SITE_ROOT constant is global, I understand that the global declaration is declared only until that script ends.

So for example, if I echo SITE_ROOT in admin.php, I will get a php error because I haven't included config.php into admin.php.

Now, is there a way to skip including config.php in each and every file and make my SITE_ROOT constant available everywhere?

TY very much! :)

Upvotes: 2

Views: 557

Answers (1)

Lee
Lee

Reputation: 13542

There is a php ini setting auto_prepend_file, which lets you specify that a given file will be automatically loaded at the start of each request.

That being said, I would strongly recommend against using that approach. If you can't require_once("config.php") at the top of each script, then an alternate approach is to use something like apache's mod_rewrite (via .htaccess) to route all requests to a single "bootstrap" script. Include your config.php from there, then analyze the request, and load the appropriate script to handle it.

Upvotes: 3

Related Questions