Re: How can I replace $_SERVER["DOCUMENT_ROOT"] in PHP 7.0?

[email protected] (Bo Berglund)
Newsgroups php.general
Message-ID <[email protected]>
On Mon, 13 Sep 2021 21:30:18 +0100, Ashley Sheridan <[email protected]>
wrote:

>
>On 13/09/2021 21:17, Bo Berglund wrote:
>> On Mon, 13 Sep 2021 20:36:35 +0100, Ashley Sheridan <[email protected]>
>> wrote:
>>
>>>> include_once ('appdbdata.php');
>>>>
>>>> THen seemingly all worked, but only until a call was made to a php file at a
>>>> deeper location which also included this file...
>>>>
>>> Have you looked at __DIR__ ?
>>> https://www.php.net/manual/en/language.constants.magic.php
>> Yes, I have tested this php file located in the htdocs/ dir:
>>
>> <?php
>> ini_set('display_errors', '1');
>> set_include_path(get_include_path().PATH_SEPARATOR.dirname($_SERVER['DOCUMENT_ROOT'],1)."/php");
>> include ('logindata.php');
>>
>> echo  "Including: " .dirname($_SERVER['DOCUMENT_ROOT'],1)."/php/logindata.php";
>> echo "<br>Mail sender= " . $mailsender ;
>> echo '<br>Dir= ' . __DIR__ ;
>> echo  "<br>DocRoot= " . dirname($_SERVER["DOCUMENT_ROOT"],1) . "/";
>> ?>
>>
>> The output was this:
>>
>> Including: /usr/services/vux/apache/php/logindata.php
>> Mail sender=
>> Dir= /data/25/2/158/91/2484743/user/2721122/htdocs
>> DocRoot= /usr/services/vux/apache/
>>
>> The strange thing is that even though I have set include_path according to the
>> selected solution in this stackoverflow thread:
>> https://stackoverflow.com/questions/44984560/what-is-the-php7-version-of-serverhome
>> it does not pick up the logindata.php file where $mailsender is defined.
>> If I move the test php file into /php then it picks up the information since it
>> is in the same dir.
>>
>> Note that __DIR__ and dirname($_SERVER['DOCUMENT_ROOT'],1) return completely
>> different paths!
>>
>> It really seems very strange that they have removed this function without
>> supplying a new version...

>They might show different paths, it all depends on the main script being 
>run (autoloaders are your friend here). But you can use it to generate 
>paths using some basic string concatenation (and some file methods if 
>you want some better error case handling).

However, my main goal was to be able to use includes across the server so php
files in different locations could use the same set of utilities by including
them with the correct path.

So since this obviously does not work I have to start calculating relative paths
from every place I have put the php files, like:

include ('../../php/logindata.php');

And what happens if such a file in turn needs an include, is it relative to the
original php file called or is it relative to the file which makes the include?


-- 
Bo Berglund
Developer in Sweden
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.