dongtan6206 2014-10-25 15:48
浏览 34

$ SERVER ['SCRIPT_NAME']与PHP 5.4中的相关项之间的差异

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:

  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'] is /test.php.

So, the question is: when will Q1-Q5 in the code above be false ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录