PHP & Hash / URL 的片段部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1162008/
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
PHP & Hash / Fragment Portion of URL
提问by mikey_w
I am trying to find a way to save the hash portion of a url and as a PHP variable. This idea is a bit kooky, but bear with me...
我试图找到一种方法来保存 url 的哈希部分并作为 PHP 变量。这个想法有点古怪,但请容忍我......
I'd like to extract the "location" fragment from the following URL and save it as a PHP variable.
我想从以下 URL 中提取“位置”片段并将其保存为 PHP 变量。
http://www.example.com/#location
However, discussion at this linkindicates that the fragment of a URL is only reachable through JavaScript.
但是,此链接上的讨论表明 URL 的片段只能通过 JavaScript 访问。
But would it be possible to create a link where the fragment is duplicated in the URL, parsed by PHP, and then removed by mod rewrite? So....
但是是否可以创建一个链接,其中片段在 URL 中重复,由 PHP 解析,然后通过 mod rewrite 删除?所以....
Original url:
原网址:
http://www.example.com/location/#location
PHP gets location variable thanks to the plain "location" in the URL
由于 URL 中的简单“位置”,PHP 获取位置变量
Apache then rewrites the link to:
Apache 然后将链接重写为:
http://www.example.com/#location
I'm curious to know if there is an elegant way to solve this problem.
我很想知道是否有一种优雅的方法来解决这个问题。
回答by pix0r
You'll need to use Javascript to read this. There are a few different options - upon page load, you could use an XmlHTTPRequest (AJAX request) to tell the server what the additional URL parameters were. Alternatley you could check to see if there are additional parameters (also via Javascript), and if you find any, post back to a different URL that has these parameters encoded into the URL itself.
您需要使用 Javascript 来阅读此内容。有几个不同的选项 - 在页面加载时,您可以使用 XmlHTTPRequest(AJAX 请求)来告诉服务器附加的 URL 参数是什么。或者,您可以检查是否有其他参数(也可以通过 Javascript),如果找到,则回发到其他 URL,该 URL 将这些参数编码到 URL 本身中。
回答by Tyler Carter
The Fragment is never sent to the server, according to this threadon the Mod_Rewrite forums. So, this might be impossible unless you use AJAX to change the page after the fact.
根据Mod_Rewrite 论坛上的这个帖子,Fragment 永远不会发送到服务器。因此,除非您事后使用 AJAX 更改页面,否则这可能是不可能的。
Another idea would be to have Javascript turn the hash into a $_GET paramater, and then refresh the page.
另一个想法是让 Javascript 将哈希转换为 $_GET 参数,然后刷新页面。
回答by dusoft
you could send hash fragment via AJAX to PHP script and do an immediate refresh (reload of the page)
您可以通过 AJAX 将哈希片段发送到 PHP 脚本并立即刷新(重新加载页面)
回答by Jerald
Once you have send the values to server via AJAX. You can set the fragment values in SESSION. When you refresh the page, you can get the fragment which was set in session and process then display the corresponding content. Because we can't get get the fragment values through PHP_SELF / QUERY_STRING and etc. We need this to increase the speed of our web page like Gmail.
一旦您通过 AJAX 将值发送到服务器。您可以在 SESSION 中设置片段值。刷新页面时,可以获取到session中设置的fragment并进行处理,然后显示相应的内容。因为我们无法通过 PHP_SELF / QUERY_STRING 等获取片段值。我们需要它来提高 Gmail 等网页的速度。
回答by Jason
It's contained in the "fragment" value returned from PHP's parse_url function.
它包含在从PHP 的 parse_url 函数返回的“片段”值中。
From PHP manual:
来自 PHP 手册:
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
Will return:
将返回:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path

