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.

Community
  • 1
  • 1
fitzilla
  • 915
  • 1
  • 9
  • 11

7 Answers7

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);
mseancole
  • 1,662
  • 4
  • 16
  • 26
2

You can use a relative path to get to it.

One level up: ../Auth.php

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

etc.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • The hint is good, but that does not always work. It works in combination with [`__DIR__`](http://www.php.net/manual/en/language.constants.predefined.php). – hakre Mar 15 '12 at 19:27
  • Hi. Thanks, but it's going to be too time consuming to sort these individually. – fitzilla Mar 15 '12 at 19:53
  • @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. – Brad Mar 15 '12 at 19:54
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

DampeS8N
  • 3,621
  • 17
  • 20
1

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

hakre
  • 193,403
  • 52
  • 435
  • 836
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';
0

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

include $_SERVER['DOCUMENT_ROOT']."/"."include_me.php";
Andrew Edvalson
  • 7,658
  • 5
  • 26
  • 24
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.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294