PHP absolute path in requireonce
Asked Answered
M

7

7

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.

Missend answered 15/3, 2012 at 19:23 Comment(1)
What's the purpose of this feature?Habsburg
C
7

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);
Celtic answered 15/3, 2012 at 19:30 Comment(0)
D
2

You can use a relative path to get to it.

One level up: ../Auth.php

Two levels up: ../../Auth.php

etc.

Doralynne answered 15/3, 2012 at 19:25 Comment(3)
The hint is good, but that does not always work. It works in combination with __DIR__.Silicon
Hi. Thanks, but it's going to be too time consuming to sort these individually.Missend
@fitzilla, In that case, you should follow hakre's or DampeS8N's solutions. They are good answers to your problem. Just remember you're making an INI change, which will need to be changed anywhere else your code is deployed.Doralynne
A
2

You should alter your php.ini to change the include path to the the path to that (all all your other) included files. Then you can include them without a path on every page regardless of their own location.

More Details

Aha answered 15/3, 2012 at 19:27 Comment(0)
S
1

Add the root level directory to your include_path - PHP will do the rest for you. No complicated variables, constants or whatever.

Silicon answered 15/3, 2012 at 19:27 Comment(0)
J
1

In addition to everything that has been said already, I suggest centralizing all common functionality. Create a file, like common.php with all includes that you need for you application and then include that from all your scripts.

Also, a nice way to do relative includes is by using dirname() function in combination with __FILE__ magic constant. For example:

require_once dirname(__FILE__) . '/../lib/common.php';
Joslin answered 15/3, 2012 at 19:45 Comment(0)
A
0

If you do not have access to php.ini, I've used something like this before

include $_SERVER['DOCUMENT_ROOT']."/"."include_me.php";
Aten answered 15/3, 2012 at 19:28 Comment(0)
T
0

The easiest way since it's a site wide change is to add its directory first in the include path in php.ini.

If you can't change php.ini, there are a few other options for adding it to the include path.

Thermoelectricity answered 15/3, 2012 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.