$ SERVER ['SCRIPT_NAME']及其相关项目之间的差异 [英] difference between $SERVER['SCRIPT_NAME'] and its related items
问题描述
这是来自yii2的 yii\web\Request
的源代码,它还引用 Zend_Controller_Request_Http
在Zend Framework和许多其他PHP框架中:
public function getScriptUrl()
{
if ($ this-> _scriptUrl === null){
// $ scriptFile = $ this-> getScriptFile();
$ scriptFile = $ _SERVER [' SCRIPT_FILENAME'];
$ scriptName = basename($ scriptFile);
// Q1:什么时候会出错?
if (basename($ _ SERVER [' SCRIPT_NAME'])=== $ scriptName){
$ this-> _scriptUrl = $ _SERVER [' SCRIPT_NAME'跨度>];
// Q2:什么时候会出错?
} elseif(basename($ _ SERVER [' PHP_SELF'])=== $ scriptName){
$ this-> _scriptUrl = $ _SERVER [' PHP_SELF'];
// 问题3:什么时候会出错?
} elseif(isset($ _ SERVER [' ORIG_SCRIPT_NAME'])&& basename($ _ SERVER [ ' ORIG_SCRIPT_NAME'])=== $ scriptName){
$ this-> _scriptUrl = $ _SERVER [' ORIG_SCRIPT_NAME'];
// 问题4:什么时候会出错?
} elseif(($ pos = strpos($ _ SERVER [' PHP_SELF'],' /'。$ scriptName))!== false){
$ this-> _scriptUrl = substr( $ _SERVER [' SCRIPT_NAME'],0,$ pos)。 ' /'。 $ scriptName ;
// 问题5:什么时候会出错?
} 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(' 无法确定条目脚本URL。');
}
}
return $ this-> _scriptUrl;
}
我试图谷歌他们,并找到以下结果:
1. 关于 $ _ SERVER $ c $的PHP文档c>
,但它没有解释Q1-Q5何时会出错。
2. PHP_SELF
vs PATH_INFO
vs SCRIPT_NAME
vs REQUEST_URI
,但我发现它已发布5年前,事情发生了变化。它提到 http://example.com/test.php/foo/bar
会使 $ _ SERVER ['PHP_SELF']
是 /test.php/foo/bar
。在我自己的电脑上运行测试后(nginx + fpm-php + php 5.4),我发现 $ _ SERVER ['PHP_SELF'] ==/ test.php
。
所以,问题是:上面代码中Q1-Q5什么时候会出错?
< blockquote> this-> _scriptUrl === null){ here is the source code from 这篇关于$ SERVER ['SCRIPT_NAME']及其相关项目之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
//
scriptFile =
this-> getScriptFile();
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:
1. the PHP doc about $_SERVER
, but it doesn't explain when Q1-Q5 would be false.
2. PHP_SELF
vs PATH_INFO
vs SCRIPT_NAME
vs REQUEST_URI
, but I found that it was posted 5 years ago, and things have changed since then. It mentioned that http://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'] == "/test.php"
.
So, the question is: when will Q1-Q5 in the code above be false ?this->_scriptUrl === null) {
//
scriptFile =
this->getScriptFile();