Portable and safe way to get PATH_INFO

后端 未结 6 2049
青春惊慌失措
青春惊慌失措 2021-01-05 04:56

I\'m seeking a portable way to receive the (handy) $_SERVER[\'PATH_INFO\'] variable.

After reading a while, it turns out PATH_INFO

6条回答
  •  渐次进展
    2021-01-05 05:28

    I didn't see the comments or the link before posting. Here is something that might work, based on what the page referenced above gives as CGI-derived variables:

    function getPathInfo() {
        if (isset($_SERVER['PATH_INFO'])) {
            return $_SERVER['PATH_INFO'];
        }  
    
        $script_filename = $_SERVER["SCRIPT_FILENAME"];
        $script_name_start = strrpos($script_filename, "/");
        $script_name = substr($script_filename, $script_name_start);
    
        //With the above you should have the plain file name of script without path        
    
        $script_uri = $_SERVER["REQUEST_URI"];
        $script_name_length = strlen($script_name);
        $path_start = $script_name_length + strpos($script_name, $script_uri);
    
        //You now have the position of where the script name ends in REQUEST_URI
    
        $pathinfo = substr($script_uri, $path_start);
        return $pathinfo;
    }
    

提交回复
热议问题