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 22:21:50 +0100, Ashley Sheridan <[email protected]>
wrote:

>But going back to your original problem, are you sure that 
>$_SERVER['DOCUMENT_ROOT'] doesn't work? I've just tested on a PHP 7.2 
>system, and it does exist and points to the document root of the 
>specific website.
>

Running with PHP 7.0.33 I have tested with the following script located in
.../htdocs/log and with other php files in .../htdocs/php:

<?php
ini_set('display_errors', '1');	//Uncomment this if there are problems...

$curdir=__DIR__;
$docroot=$_SERVER["DOCUMENT_ROOT"];
$phpdir=$_SERVER['DOCUMENT_ROOT'] . "/php";

echo "<br>1. curdir: " . $curdir;
echo "<br>2. docroot: " . $docroot;
echo "<br>3. phpdir: " . $phpdir;
echo "<br>Testing includes:";
echo "<br>Mail sender= " . $mailsender ;
echo "<br>Set include: " . $_SERVER["DOCUMENT_ROOT"] . "/php/logindata.php";
include ($_SERVER['DOCUMENT_ROOT'] . "/php/logindata.php");
echo "<br>Mail sender from php dir= " . $mailsender ;
echo "<br>Set include: " . '../php/logindata.php';
include ('../php/logindata.php');
echo "<br>Mail sender from php dir= " . $mailsender ;
?>

And the result comes out like this:

1. curdir: /data/25/2/158/91/2484743/user/2721122/htdocs/log
2. docroot: /usr/services/vux/apache/htdocs
3. phpdir: /usr/services/vux/apache/htdocs/php
Testing includes:
Mail sender=
Set include: /usr/services/vux/apache/htdocs/php/logindata.php
Mail sender from php dir=
Set include: ../php/logindata.php
Mail sender from php dir= [email protected]

Notice the vast difference between __DIR__ and $_SERVER['DOCUMENT_ROOT']!
They are not even close!

The only working path is what I get from __DIR__ but then I have to somehow
reduce that to the document root (htdocs) and then tack on /php in order to get
to the proper include location.

But I have tried to use variables in include statements but they seem only to
accept constants, so I cannot even make a function to return the actual path to
htdocs and use that to set the include...

Without a possibility to get the correct path to htdocs it seems impossible to
create a php file which can be put in any site dir and still include files from
the htdocs/php directory.
This is what I need to do...

/usr/services/vux/apache/htdocs is NOT the same as
/data/25/2/158/91/2484743/user/2721122/htdocs

Only the latter works in include statements.

-- 
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.