我可以在服务器端应用程序(PHP,Ruby,Python等)上读取URL的哈希部分吗?

Rob*_*cks 212 language-agnostic url http uri-fragment web

假设URL为:

www.example.com/?val=1#part2
Run Code Online (Sandbox Code Playgroud)

PHP可以val1使用GET数组读取请求变量.

哈希值是否part2也可读?或者这只是浏览器和JavaScript?

Ion*_*tan 199

主要问题是浏览器甚至不会发送带有片段部分的请求.片段部分在浏览器中解析.所以它可以通过JavaScript访问.

无论如何,您可以使用parse_url()将URL解析为位,包括片段部分,但显然不是您的情况.


tom*_*tom 98

简单测试,访问http:// localhost:8000/hello?foo = bar #this-is-not-sent-to-server

python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

服务器在没有#appendage的情况下接收请求 - 在哈希标记之后的任何内容都只是客户端上的锚点查找.

您可以通过javascript使用URL中找到锚名称,例如:

<script>alert(window.location.hash);</script>
Run Code Online (Sandbox Code Playgroud)

如果您已经拥有包含片段(http://codepad.org/BDqjtXix)所需的URL字符串,PHP中的parse_url()函数可以工作:

<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>

Output: fizzbuzz
Run Code Online (Sandbox Code Playgroud)

但我不认为PHP接收片段信息,因为它只是客户端.


Ali*_*man 48

它可以从Javascript中检索 - as window.location.hash.从那里你可以用Ajax将它发送到服务器,或者编码并将其放入URL然后可以传递到服务器端.

  • 谢谢!因此,需要明确的是,您必须在服务器上重定向到它,而不是:`www.example.com/?val=1#part2`,如下所示:`www.example.com/?redirectUrl=%2F %3Fval%3D1%23part2`,当然,您必须添加支持以重定向到其他页面中的其他网址。不太好,当然并不适用于所有用例,但适用于书签。请注意,如果您这样做,则不应允许重定向到绝对网址,而只允许重定向到相对网址,以确保您不会暴露于[不安全的重定向](https://www.owasp.org/index.php/Unvalidated_Redirects_and_Forwards_Cheat_Sheet) (2认同)

Pat*_*and 26

哈希从不发送到服务器,所以没有.


小智 8

是的,这是真的,服务器没有获得锚点部分.但是有一种使用cookie的解决方法.你可以在这里找到它:http://www.stoimen.com/blog/2009/04/15/read-the-anchor-part-of-the-url-with-php/

  • 它怎么没价值?这绝对不值钱.但也许有更好的技术.很好. (11认同)
  • 这种"解决方法"毫无价值.如果每页视图有2个请求,那么有十几种更好的技术. (2认同)

Qin*_*iso 7

答案是不.

哈希的主要目的是滚动到已定义书签的页面的某个部分.例如,页面加载时滚动到此部分.

浏览将滚动,使得此行是页面中的第一个可见内容,具体取决于该行下方有多少内容.

是的,javascript可以访问它,然后一个简单的ajax调用将做的魔术


sam*_*jco 5

关于什么:

动态获取#hash

<script>
var urlhash = window.location.hash, //get the hash from url
    txthash = urlhash.replace("#", ""); //remove the #
    //alert(txthash);
</script>

<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
Run Code Online (Sandbox Code Playgroud)

为了让它更流畅:

仅使用 Javascript 和 PHP 的完整示例

<script>
var urlhash = window.location.hash,  //get the hash from url
txthash = urlhash.replace("#", "");  //remove the #

function changehash(a,b){
   window.location.hash = b; //add hash to url
   //alert(b); //alert to test
   location.reload(); //reload page to show the current hash
}
</script>

<?php $hash = "<script>document.writeln(txthash);</script>";?>

<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/> 

<?php echo "This is the current hash: " . $hash; ?> 
Run Code Online (Sandbox Code Playgroud)