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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 14/09/2021 08:57, Bo Berglund wrote:
On Mon, 13 Sep 2021 22:21:50 +0100, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx>
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= donald@xxxxxxxx

Notice the vast difference between __DIR__ and $_SERVER['DOCUMENT_ROOT']!
They are not even close!
They're not meant to be the same, I suggested using __DIR__ as you said you had problems with $_SERVER['DOCUMENT_ROOT'], suggesting it was not set

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.
These are two completely different paths. Are you certain of your directory structure across both versions of PHP?
Take the problem right down to the most basic elements.

* You have a problem with relative paths using $_SERVER['DOCUMENT_ROOT']
* $_SERVER['DOCUMENT_ROOT'] should not be changing across scripts in a website, suggesting the problem might be that it's not set to what you think it is * Confirm $_SERVER['DOCUMENT_ROOT'] is exactly what you think it should be across both versions

If you go through the basic steps, you should see what the problem is.

--
Ashley Sheridan
https://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux