【问题标题】:How to print current URL path?如何打印当前 URL 路径?
【发布时间】:2013-02-01 12:06:57
【问题描述】:

我想打印出当前的 URL 路径,但我的代码不能正常工作。

我在我的 file.php 中使用它

echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];

当我打开网址 http://sub.mydomain.com/file.php 时,它似乎工作正常,并打印出"http://sub.mydomain.com/file.php"

但如果我删除了 .php 扩展名,因此 url 将改为 http://sub.mydomain.com/file,它会打印 "http://sub.mydomain.com/sub/file.php" 这是错误的。

子域打印两次,不知道为什么?

在我的 .htaccess 文件中,我进行了重写,可以删除 .php 扩展名。

有谁可以/想帮助我吗? :)

【问题讨论】:

    标签: php url


    【解决方案1】:

    您需要$_SERVER['REQUEST_URI'] 而不是$_SERVER['SCRIPT_NAME'],因为$_SERVER['SCRIPT_NAME'] 将始终为您提供当前正在工作的文件。

    来自手册:

    SCRIPT_NAME:包含当前脚本的路径。这对于需要指向自身的页面很有用。 __FILE__ 常量包含当前(即包含的)文件的完整路径和文件名。 .

    我想这可以帮助您完全获取当前 URL。

    echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    

    注意:不要依赖客户的HTTP_HOST,而是使用SERVER_NAME!见:What is the difference between HTTP_HOST and SERVER_NAME in PHP?

    安全警告

    如果您在任何地方使用它(打印或存储在数据库中),您需要过滤(清理)$_SERVER['REQUEST_URI'],因为它不安全。

    // ie: this could be harmfull
    /user?id=123%00%27<script...
    

    因此,请始终在使用用户输入之前对其进行过滤。至少使用htmlspecialchars、htmlentities、strip_tags等。

    或者类似的东西;

    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(["\0", '%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']));
    }
    

    【讨论】:

    • 如果您的真实服务主机在代理服务器之后或使用非标准端口,则依赖 HTTP_HOST 非常有用
    【解决方案2】:
    $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'], '/'). '/';
    }
    

    【讨论】:

    • 描述你的答案
    • 如何打印当前 URL 路径? $current_url -> 当前 URL 路径
    • 在 MacOS Sierra 上的最新 MAMP 上失败。 stackoverflow.com/questions/18008135/…
    • $scheme = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http'; if( $main_folder ) { $current_url = $scheme.'://'.$_SERVER['SERVER_NAME']. '/' 。 ltrim($main_folder, '/') 。 '/'; } else { $current_url = $scheme.'://'.rtrim($_SERVER['SERVER_NAME'], '/')。 '/'; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多