ChatGPT解决这个技术问题 Extra ChatGPT

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

假设 URL 为:

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

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

哈希值 part2 是否也可读?还是这仅取决于浏览器和 JavaScript?


I
Ionuț G. Stan

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

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


t
tom

简单测试,访问 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 -

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

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

<script>alert(window.location.hash);</script>

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

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

Output: fizzbuzz

但我不认为 PHP 接收到片段信息,因为它是仅限客户端的。


A
Alister Bulman

它可以从 Javascript 中检索 - 作为 window.location.hash。例如,您可以从那里使用 Ajax 将其发送到服务器,或者对其进行编码并将其放入 URL,然后可以将其传递到服务器端。


谢谢!所以要清楚,而不是这样:www.example.com/?val=1#part2 您必须在服务器上重定向到它,如下所示:www.example.com/?redirectUrl=%2F%3Fval%3D1%23part2 当然,您必须添加支持以重定向到您的其他网址中的其他网址页。不是很棒,当然也不适用于所有用例,但确实适用于书签。请注意,如果您这样做,则不应允许重定向到绝对 url,只允许重定向到相对 url,以确保您不会打开自己到 unsafe redirects
P
PatrikAkerstrand

哈希永远不会发送到服务器,所以不会。


Q
Qiniso

答案是不。

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

浏览将滚动,以使该行成为页面中的第一个可见内容,具体取决于该行下方的内容量。

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


s
samjco

关于什么:

动态抓取#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;
?>

为了使其更流畅:

仅使用 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; ?> 

PHP 仍然没有得到哈希值。
甜蜜而简短的解决方案。
S
Silfverstrom

我认为哈希值仅在客户端使用,因此您无法使用 php 获取它。

不过,您可以使用 javascript 将其重定向到 php。


C
Community

# 之后的 URI 部分称为“片段”,根据定义仅在客户端可用/处理(请参阅 https://en.wikipedia.org/wiki/Fragment_identifier)。

在客户端,可以使用带有 window.location.hash 的 javaScript 访问它。


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

这应该工作


如果您将 url 作为字符串很容易,即使您使用 reg exp 解析它也是如此。问题是您无法从服务器访问 #photo45(使用 phpinfo() 并且您不会在任何地方看到 #photo45
这对该页面的初始请求没有任何作用
A
Aurora

是的你可以:

使用此方法来防止错误:

<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

这对该页面的初始请求没有任何作用
K
Keshav Kalra

我们也可以用另一种方法来做到这一点,首先从 js 中获取哈希值并使用该参数调用 ajax 并可以做任何我们想做的事情


M
Matteo Conta

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

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

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

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

在服务器端的 php 中,您可以使用 $_POST 集合读取此值:

$server_location_hash = $_POST['myHiddenLocationHash'];

这对该页面的初始请求没有任何作用
我同意,它适用于后续请求,而不适用于第一次加载。