php 使用#tag 获取完整网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6119095/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:23:47  来源:igfitidea点击:

Getting FULL URL with #tag

php

提问by raladin

Tried to print the $_SERVER array in PHP, yet I can't find what I want:

试图在 PHP 中打印 $_SERVER 数组,但我找不到我想要的:

http://www.domain.com/#sometaginpage

http://www.domain.com/#sometaginpage

I want the "sometaginpage".

我想要“某个页面”。

Help. Thanks!

帮助。谢谢!

回答by Sean Walsh

The browser doesn't actually send anything that comes after the hash(#) to the server because it is resolved within the browser.

浏览器实际上不会将 hash(#) 之后的任何内容发送到服务器,因为它是在浏览器中解析的。

回答by drudge

Fairly certain that #hashtagsare NOT sent to the server, but you could develop a workaround with AJAX:

相当确定#hashtags不会发送到服务器,但您可以使用 AJAX 开发一种解决方法:

some-page.html:

some-page.html

<script type="text/javascript">
    $(document).ready(function() {
        $(window).bind('hashchange', function() {
            var hash = window.location.hash.substring(1);
            $.get('ajax-hash.php', { tag: hash },
                function(data) { $('#tag').html(data); }
            );
        });
    });
</script>

<div id="tag"></div>
<a href="#one">#one</a> | <a href="#two">#two</a> | <a href="#lolwut">#lolwut</a>

ajax-hash.php:

ajax-hash.php

<?php
    $hash = isset($_GET['tag']) ? $_GET['tag'] : 'none';
    echo $_SERVER['HTTP_REFERER'] . '#' . $hash;
?>

Note:This is dependent on the browser actually sending the HTTP_REFERER.. Since it's done through jQuery, it SHOULD.. but no promises! (Antivirus/Firewalls love to strip that from your packets)

注意:这取决于实际发送HTTP_REFERER..的浏览器,因为它是通过 jQuery 完成的,它应该 .. 但没有承诺!(防病毒/防火墙喜欢从您的数据包中删除它)

回答by John

I'm not sure if $_SERVER['REQUEST_URI'] will give it or not. s992 might be right, it may not send that to the server.

我不确定 $_SERVER['REQUEST_URI'] 是否会给它。s992 可能是对的,它可能不会将其发送到服务器。