php 如何打印当前的 URL 路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14912943/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print current URL path?
提问by TheYaXxE
I want to print out the current URL path, but my code doesn't work propperly.
我想打印出当前的 URL 路径,但我的代码不能正常工作。
I use this in my file.php
我在我的 file.php 中使用它
echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
When i open the url http://sub.mydomain.com/file.phpit seems to work fine, and it prints "http://sub.mydomain.com/file.php"
当我打开 url http://sub.mydomain.com/file.php它似乎工作正常,它打印"http://sub.mydomain.com/file.php"
But if i remove the .php extension so the url will be http://sub.mydomain.com/fileinstead, it prints "http://sub.mydomain.com/sub/file.php"which is wrong.
但是,如果我删除 .php 扩展名,那么 url 将改为http://sub.mydomain.com/file,它会打印"http://sub.mydomain.com/sub/file.php"哪个是错误的。
It prints the subdomain twice, and I don't know why?
它打印子域两次,我不知道为什么?
In my .htaccess file, I have a rewrite that makes it possible to removes .php extensions.
在我的 .htaccess 文件中,我进行了重写,可以删除 .php 扩展名。
Anyone who can/want to help me please? :)
有人可以/想要帮助我吗?:)
回答by K-Gun
You need $_SERVER['REQUEST_URI']instead of $_SERVER['SCRIPT_NAME'], cos $_SERVER['SCRIPT_NAME']will always give you the file which is working at the moment.
您需要$_SERVER['REQUEST_URI']而不是$_SERVER['SCRIPT_NAME'], cos$_SERVER['SCRIPT_NAME']将始终为您提供当前正在运行的文件。
From manual:
从手册:
SCRIPT_NAME: Contains the current script's path. This is useful for pages which need to point to themselves. The
__FILE__constant contains the full path and filename of the current (i.e. included) file. .
SCRIPT_NAME:包含当前脚本的路径。这对于需要指向自身的页面很有用。该
__FILE__常量包含当前(即包含的)文件的完整路径和文件名。.
I suppose this helps you getting current URL fully.
我想这可以帮助您完全获取当前 URL。
echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Notice: DO NOT RELY ON CLIENT'S HTTP_HOST, USE SERVER_NAMEINSTEAD! SEE: What is the difference between HTTP_HOST and SERVER_NAME in PHP?
注意:不要依赖客户HTTP_HOST,而是使用SERVER_NAME!参见:PHP 中的 HTTP_HOST 和 SERVER_NAME 有什么区别?
Security Warning
安全警告
You need to filter (sanitize) $_SERVER['REQUEST_URI']if you use it in anywhere (to print or store in database), cos it's not safe.
$_SERVER['REQUEST_URI']如果你在任何地方使用它(打印或存储在数据库中),你需要过滤(消毒),因为它不安全。
// ie: this could be harmfull
/user?id=123%00%27<script...
Hence, always filter user inputs before using them. At least use htmlspecialchars, htmlentities, strip_tagsetc..
因此,在使用之前总是过滤用户输入。至少使用htmlspecialchars,htmlentities,strip_tags等。
Or something like this;
或类似的东西;
function get_current_url($strip = true) {
    static $filter, $scheme, $host, $port; 
    if ($filter == null) {
        $filter = function($input) use($strip) {
            $input = trim($input);
            if ($input == '/') {
                return $input;
            }
            // add more chars if needed
            $input = str_ireplace(["$main_folder = str_replace('\','/',dirname(__FILE__) );
$document_root = str_replace('\','/',$_SERVER['DOCUMENT_ROOT'] );
$main_folder = str_replace( $document_root, '', $main_folder);
if( $main_folder ) {
    $current_url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']. '/' . ltrim( $main_folder, '/' ) . '/';
} else {
    $current_url = $_SERVER['REQUEST_SCHEME'].'://'.rtrim( $_SERVER['SERVER_NAME'], '/'). '/';
}
", '%00', "\x0a", '%0a', "\x1a", '%1a'], '',
                rawurldecode($input));
            // remove markup stuff
            if ($strip) {
                $input = strip_tags($input);
            }
            // or any encoding you use instead of utf-8
            $input = htmlspecialchars($input, ENT_QUOTES, 'utf-8');
            return $input;
        };
        $scheme = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME']
            : ('http'. (($_SERVER['SERVER_PORT'] == '443') ? 's' : ''));
        $host = $_SERVER['SERVER_NAME'];
        $port = ($_SERVER['SERVER_PORT'] != '80' && $scheme != 'https')
            ? (':'. $_SERVER['SERVER_PORT']) : '';
        }
    }
    return sprintf('%s://%s%s%s', $scheme, $host, $port, $filter($_SERVER['REQUEST_URI']));
}

