0

I just figured out that I can place files outside the domain root (outside public_html).

The benefit of placing functions outside the domain root is that I can use the same file on multiple domains. If I update my file in a single location, all my domains get updated.

Like this:

include __DIR__ . '/../../my-hidden-folder/functions.php';

My question is, now that it's possible, is it a good idea to work like this? Or are there reasons why this is not a good approach?

Not that it probably matters but I use PHP, Apache and Cpanel.

1 Answers1

1

In general it is good practice to only store data that needs to be directly accessible by an URL in your web root and everything that is not should be stored outside the web root.

The benefit of placing functions outside the domain root is that I can use the same file on multiple domains.

That is not quite true. The include() function doesn't care if files are inside or outside a web root, or if logically that file-system directory used as the web root for a different site...

HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • Yes, I agree. What do you think of the idea to share files (include functions) between domains in a folder below the public_html? Any pitfalls? – Jens Törnell Sep 20 '18 at 05:44
  • This is good practice (code-reuse). However, keep in mind that if you need to make a change to one of these functions while working with domain A, and you inadvertently change it in a way that domain B is now broken (such as you removed one of the function arguments, and updated domain A, but forgot all about domain B). – reformed Aug 09 '19 at 20:45