note 71003 added to function.ob-start

From: Date: Tue, 07 Nov 2006 23:35:34 +0000
Subject: note 71003 added to function.ob-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-119492@lists.php.net to get a copy of this message
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 ---- Server IP: 216.235.15.211 Probable Submitter: 70.80.9.195 ---- Manual Page -- http://www.php.net/manual/en/function.ob-start.php Edit -- https://master.php.net/note/edit/71003 Del: integrated -- https://master.php.net/note/delete/71003/integrated Del: useless -- https://master.php.net/note/delete/71003/useless Del: bad code -- https://master.php.net/note/delete/71003/bad+code Del: spam -- https://master.php.net/note/delete/71003/spam Del: non-english -- https://master.php.net/note/delete/71003/non-english Del: in docs -- https://master.php.net/note/delete/71003/in+docs Del: other reasons-- https://master.php.net/note/delete/71003 Reject -- https://master.php.net/note/reject/71003 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119492) next »