假设URL为:

www.example.com/?val=1#part2

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

哈希值part2也是可读的吗?或者这仅仅取决于浏览器和JavaScript?


当前回答

<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>

这应该可以

其他回答

我认为哈希值只在客户端使用,所以在php中无法获得。

你可以用javascript重定向到PHP。

答案是否定的。

散列的主要目的是滚动到已定义书签的页面的某个部分。当页面加载时,滚动到此部分。

浏览将滚动,使该行成为页面中第一个可见内容,这取决于该行以下有多少内容。

是的,javascript可以访问它,然后一个简单的ajax调用就可以做到这一点

是的,你可以:

使用此方法可以防止错误:

<script> 
query=location.hash;
document.cookie= 'anchor'+query;
</script>

当然,在PHP中,爆炸那个小狗,得到其中一个值

$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag

另一个解决方案是在php页面中添加一个隐藏的输入字段:

<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">

使用javascript/jQuery,你可以在页面加载或响应事件时设置该字段的值:

$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));

在php的服务器端,你可以使用$_POST集合读取这个值:

$server_location_hash = $_POST['myHiddenLocationHash'];

哈希值不会被发送到服务器,所以不会。