note 71003 deleted from function.ob-start by cmb
| From: | cmb@php.net | Date: | Mon, 21 Jan 2019 15:29:41 +0000 |
| Subject: | note 71003 deleted from function.ob-start by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210096@lists.php.net to get a copy of this message | ||
Note Submitter: feedback at realitymedias dot com
----
PHP Suggests: Some web servers (e.g. Apache) change the working directory of a script when calling
the callback function. You can change it back by doing, for example, the following in the callback
function: chdir(dirname($_SERVER['SCRIPT_FILENAME']))
The solution provided by PHP, does not function as intended when running PHP as a CGI (on CGI mode
(CGI-BUILD (--enable-cgi) and/or CLI)). In such a case, PHP is executed as a CGI-BIN and the web
server daemon (e.g. Apache) sees SCRIPT_FILENAME as being the PHPCGI processor, and won't look
deeper to find what file the PHPCGI processor is actually running/parsing; therefore the path
returned by SCRIPT_FILENAME is wrong (most of the time, containing/ending with
"cgi-system/php.cgi").
As SCRIPT_FILENAME is the safest way to proceed, but turns to be wrong in this exact situation;
PATH_TRANSLATED is the next safe solution one would turn towards since it is populated with a
different mechanism.
It would be correct to develop in the direction if the script filename itself is contained in the
SCRIPT_FILENAME path value, then the SCRIPT_FILENAME content is reported correctly. If it is not,
using PATH_TRANSLATED is the next logical choice we can use. The best reference in this case would
be PHP_SELF as it is populated by PHP itself. Using SCRIPT_NAME as a reference would be an error as
it is affected by the same problem (reports cgi-system and/or php.cgi as well).
The following is the revised code and should work on both the non-CGI and the CGI PHP processor
types.
<?php
chdir(dirname((strstr($_SERVER["SCRIPT_FILENAME"], $_SERVER["PHP_SELF"])
? $_SERVER["SCRIPT_FILENAME"] : $_SERVER["PATH_TRANSLATED"])));
?>
Or the decomposed code as follows:
<?php
if (strstr($_SERVER["SCRIPT_FILENAME"], $_SERVER["PHP_SELF"])) {
$reference = $_SERVER["SCRIPT_FILENAME"];
} else {
$reference = $_SERVER["PATH_TRANSLATED"];
}
chdir(dirname($reference);
?>
This has been tested on Apache 1 & 2, PHP 4 & 5 and IIS 5.1