here is the source code from yii\web\Request
of yii2, and it also references Zend_Controller_Request_Http
in the Zend Framework and many other PHP frameworks:
public function getScriptUrl()
{
if ($this->_scriptUrl === null) {
//$scriptFile = $this->getScriptFile();
$scriptFile = $_SERVER['SCRIPT_FILENAME'];
$scriptName = basename($scriptFile);
// Q1: when will this be false ?
if (basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
$this->_scriptUrl = $_SERVER['SCRIPT_NAME'];
// Q2: when will this be false ?
} elseif (basename($_SERVER['PHP_SELF']) === $scriptName) {
$this->_scriptUrl = $_SERVER['PHP_SELF'];
// Q3: when will this be false ?
} elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
$this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME'];
// Q4: when will this be false ?
} elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
$this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
// Q5: when will this be false ?
} elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) {
$this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile));
} else {
throw new InvalidConfigException('Unable to determine the entry script URL.');
}
}
return $this->_scriptUrl;
}
I have tried to google them, and found the following results:
- the PHP doc about
$_SERVER
, but it doesn't explain when Q1-Q5 would be false. PHP_SELF
vsPATH_INFO
vsSCRIPT_NAME
vsREQUEST_URI
, but I found that it was posted 5 years ago, and things have changed since then. It mentioned thathttp://example.com/test.php/foo/bar
would make$_SERVER['PHP_SELF']
be/test.php/foo/bar
. After running a test on my own PC (nginx + fpm-php + php 5.4), I find that$_SERVER['PHP_SELF']
is/test.php
.
So, the question is: when will Q1-Q5 in the code above be false ?