Search code examples
phpabsoluterequire-once

PHP absolute path in requireonce


I'm using a simple pre-made authorisation tool to secure my site. The tool requires this line of code to be applied to the top of every .php page. Auth.php lives on the root level.

<?php ${(require_once('Auth.php'))}->protectme(); ?>

I need to be able to add this line of code to every file, including files in sub-folders. But I'm unsure of how to apply the method as shown in require_once in php to ensure it always is linked absolutely, alongside the protectme(); function.

Any enlightenment is appreciated.

Cheers.


Solution

  • Set a variable to the absolute path. $_SERVER['DOCUMENT_ROOT'] is a PHP global variable that stores the servers, root path. You just need to insert the rest of the path information to the script. For instance if your website exists on /var/www/ and your script exists at /var/www/scripts you would do the following.

    $path = $_SERVER['DOCUMENT_ROOT'] . '/scripts/Auth.php';
    require_once($path);