假设URL为:
www.example.com/?val=1#part2
PHP可以使用GET数组读取请求变量val1。
哈希值part2也是可读的吗?或者这仅仅取决于浏览器和JavaScript?
假设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中检索-作为window.location.hash。从那里,你可以用Ajax将其发送到服务器,或者将其编码并放入url中,然后可以传递到服务器端。
是什么:
动态抓取#散列
<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;
?>
为了使它更流利:
使用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; ?>
简单的测试,访问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 -
服务器接收请求时不带#附件——散列标签后的任何内容都只是客户端上的锚查找。
你可以通过javascript找到URL中使用的锚名,例如:
<script>alert(window.location.hash);</script>
PHP中的parse_url()函数可以工作,如果您已经有了所需的URL字符串,包括片段(http://codepad.org/BDqjtXix):)
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
但我认为PHP不会接收片段信息,因为它是客户机专用的。
哈希值不会被发送到服务器,所以不会。