phpincludeinclude-pathphp-include

How to PHP-Include from Root of Domain?


I have a file.php located in www.example.com/folder/.

I want that file to use include command for files located in www.example.com/include/.

If I simply use:

<?php include './include/footer.php';?>

It doesn't work, as it will try to search in the physical folder where the file.php is located, while I need it to refer to the root of my domain.


Solution

  • Use $_SERVER['DOCUMENT_ROOT']

    Here's the server documentation.

    The document root directory under which the current script is executing, as defined in the server's configuration file.

    Together, it should look like this:

    include $_SERVER['DOCUMENT_ROOT'] . "/include/footer.php";