javascript 如何使用 PHP 在地址栏上获取完整 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4768181/
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 get full URL on the address bar using PHP
提问by ptamzz
Possible Duplicate:
Get entire URL, including query string and anchor
可能重复:
获取整个 URL,包括查询字符串和锚点
I have my url like www.domain.com/#!/username(just like in twitter).
我的网址如www.domain.com/#!/username(就像在 Twitter 中一样)。
How can I retrieve the full URL (the whole string above) including the substring after '#!' in the address using PHP or Javascript??
如何检索包含“#!”之后的子字符串的完整 URL(上面的整个字符串)在地址中使用PHP或Javascript??
采纳答案by David says reinstate Monica
In JavaScript, unless you've got something else going on, you should be able to get the full URL from document.location, (so var URLstring = document.location;for example).
在 JavaScript 中,除非您有其他事情要做,否则您应该能够从document.location,获取完整的 URL (var URLstring = document.location;例如)。
In PHP, as others have noted, this is impossible due to the way the hash works (it navigates within the page, and does not trigger a page-reload so the server never knows about it, unless the JS calls an Ajax function that triggers a script on the server).
在 PHP 中,正如其他人所指出的,由于哈希的工作方式,这是不可能的(它在页面内导航,并且不会触发页面重新加载,因此服务器永远不会知道它,除非 JS 调用触发的 Ajax 函数)服务器上的脚本)。
回答by Frode
The hash part of the URL (the # and everything after) isn't sent by the browser to the server, so it's not available to PHP.
URL 的哈希部分(# 和后面的所有内容)不会由浏览器发送到服务器,因此 PHP 无法使用它。
If you absolutely need your server side to be aware of it, the only way is to load an intermediate page containing Javascript that reads the hash value, and makes another request where the value is contained in a regular HTTP parameter. E.g. the intermediate page could be something like:
如果您绝对需要您的服务器端意识到这一点,唯一的方法是加载一个包含读取哈希值的 Javascript 的中间页面,并发出另一个请求,其中该值包含在常规 HTTP 参数中。例如中间页面可能是这样的:
<html>
    <!-- snip.. -->
    <body>
        <script type="text/javascript">
            $(document).ready(function() {
                $.get('realContent.php?hash=' + location.hash, 
                    function(data) {
                        $('#content').html(data);
                     }
                );
             }
        </script>
        <div id="content"></div>
    </body>
</html>
回答by Manish Trivedi
I used this function for this(PHP 5.2)::
我为此使用了这个函数(PHP 5.2)::
function getInstance($uri = 'SERVER')
    {
        if ($uri == 'SERVER')
        {
            if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
                $https = 's://';
            } else {
                $https = '://';
            }
            if (!empty ($_SERVER['PHP_SELF']) && !empty ($_SERVER['REQUEST_URI'])) {
                $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                if (strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING']) === false) {
                    $theURI .= '?'.$_SERVER['QUERY_STRING'];
                }
            }
             else
             {
                $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
                if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
                    $theURI .= '?' . $_SERVER['QUERY_STRING'];
                }
            }
            $theURI = urldecode($theURI);
            $theURI = str_replace('"', '"',$theURI);
            $theURI = str_replace('<', '<',$theURI);
            $theURI = str_replace('>', '>',$theURI);
            $theURI = preg_replace('/eval\((.*)\)/', '', $theURI);
            $theURI = preg_replace('/[\\"\\'][\s]*javascript:(.*)[\\"\\']/', '""', $theURI);
    }
    echo (string)$theURI;
}
回答by Viktor
The part after the hash (#) is never sent to the server so you will never receive the "full" url back to PHP. The part after the # is only used on the client side and can only be sent from a server to the client.
散列 (#) 之后的部分永远不会发送到服务器,因此您永远不会收到返回给 PHP 的“完整”url。#后面的部分只在客户端使用,只能从服务器发送到客户端。
回答by edgester
Sled's answer is the best you can do, however, there is no way to get text after a pound sign "#". Anything after a "#" is considered an anchor and is not sent to the server. The "#" is only interpreted by the browser. You might try some Javascript trickery here, but I recommend avoiding passing data after a "#".
Sled 的答案是您能做的最好的回答,但是,无法在井号“#”之后获取文本。“#”之后的任何内容都被视为锚点,不会发送到服务器。“#”仅由浏览器解释。您可以在这里尝试一些 Javascript 技巧,但我建议避免在“#”之后传递数据。

