Absolute PHP path in requireonce

I use a simple pre-authorization tool to protect my site. The tool requires this line of code to be applied to the top of each .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 subfolders. But I'm not sure how to apply the method, as shown in require_once in php , to make sure that it is always absolutely connected, along with protection (); function.

Any enlightenment is appreciated.

Greetings.

+6
source share
8 answers

Set the variable to an absolute path. $ _SERVER ['DOCUMENT_ROOT'] is the global PHP variable in which the servers are stored, the root path. You just need to insert the rest of the path information into the script. For example, if your site exists in / var / www / and your script exists in / var / www / scripts, you should do the following.

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

You can use the relative path to get to it.

One level up: ../Auth.php

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

and etc.

+2
source

You must modify your php.ini to change the inclusion path to the path to this (all other) included files. You can then turn them on without a path on each page, regardless of their own location.

More details

+2
source

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

+1
source

In addition to everything that has already been said, I propose to centralize all common functions. Create a file, for example common.php, and all this includes the application you need, and then include it from all your scripts.

In addition, a good way to make relative involves using the dirname() function in conjunction with the __FILE__ magic constant. For instance:

 require_once dirname(__FILE__) . '/../lib/common.php'; 
+1
source

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

 include $_SERVER['DOCUMENT_ROOT']."/"."include_me.php"; 
0
source

The easiest way, since this is a site change, is to add its directory first to the include path in php.ini .

If you cannot change php.ini , there are several other options for adding it to the inclusion path .

0
source

Why not just use the absolute path?

 require('/auth.php'); 

Perhaps I do not understand your question

-1
source

Source: https://habr.com/ru/post/910843/


All Articles