如何打印当前的URL路径? [英] How to print current URL path?

查看:239
本文介绍了如何打印当前的URL路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印出当前的URL路径,但是我的代码无法正常工作.

I want to print out the current URL path, but my code doesn't work propperly.

我在我的file.php中使用它

I use this in my file.php

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

当我打开网址 http://sub.mydomain.com/file.php 似乎可以正常工作,并显示"http://sub.mydomain.com/file.php"

但是,如果我删除了.php扩展名,则该网址将为 http://sub.mydomain. com/file ,它会打印错误的"http://sub.mydomain.com/sub/file.php".

But if i remove the .php extension so the url will be http://sub.mydomain.com/file instead, it prints "http://sub.mydomain.com/sub/file.php" which is wrong.

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

It prints the subdomain twice, and I don't know why?

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

In my .htaccess file, I have a rewrite that makes it possible to removes .php extensions.

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

Anyone who can/want to help me please? :)

推荐答案

您需要$_SERVER['REQUEST_URI']而不是$_SERVER['SCRIPT_NAME'],cos $_SERVER['SCRIPT_NAME']将始终为您提供当前可以使用的文件.

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.

摘自手册:

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

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. .

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

I suppose this helps you getting current URL fully.

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

注意:请勿依赖客户端的HTTP_HOST,请使用SERVER_NAME代替! SEE: PHP中的HTTP_HOST和SERVER_NAME有什么区别?

Notice: DO NOT RELY ON CLIENT'S HTTP_HOST, USE SERVER_NAME INSTEAD! SEE: What is the difference between HTTP_HOST and SERVER_NAME in PHP?

如果要在任何地方(打印或存储在数据库中)使用$_SERVER['REQUEST_URI'],则需要对其进行过滤(消毒),因为这是不安全的.

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.

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

因此,请始终在使用前过滤用户输入.至少使用htmlspecialcharshtmlentitiesstrip_tags等.

Hence, always filter user inputs before using them. At least use htmlspecialchars, htmlentities, strip_tags etc..

或者类似的东西

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']));
}

这篇关于如何打印当前的URL路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆