Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
PHP
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 ?
Posted

1 solution

http://www.abc.com/example/index.php/check/issue[^]

If we run, $_SERVER['PHP_SELF'], it will result as '/example/index.php/check/issue';

If we run, $_SERVER['SCRIPT_NAME'], it will result as '/example/index.php';

If we run, $_SERVER['DOCUMENT_ROOT'], it will result the document root directory under which the current script is executing, as defined in the server's configuration file. For example, E:/projects/abc/example.

But, no idea about $_SERVER['ORIG_SCRIPT_NAME'] and to be honest, heard the word 'ORIG_SCRIPT_NAME' first time :).

Hope this one will help you :).
 
Share this answer
 
Comments
linuor 28-Oct-14 7:40am    
thanks (:

but pity that you just told the case of basename($_SERVER['SCRIPT_NAME']) === basename($_SERVER['SCRIPT_FILENAME']) , not the case !== .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900