如何通过$_GET在URL中接受一个散列标记?

时间:2021-11-24 05:22:42

From what I have been able to understand, hash marks (#) aren't sent to the server, so it doesn't seem likely that I will be able to use raw PHP to parse data like in the URL below:

从我能够理解的内容来看,散列标记(#)并没有发送到服务器,因此我似乎不太可能使用原始PHP来解析如下URL中的数据:

index.php?name=Ben&address=101 S 10th St Suite #301

I'm looking to pre-populate form fields with this $_GET data. How would I do this with Javascript (or jQuery), and is there a fallback that wouldn't break my form for people not using Javascript? Currently if there is a hash (usually in the address field), everything after that is not parsed in or stored in $_GET.

我希望用这个$_GET数据预填充表单字段。我将如何使用Javascript(或jQuery)进行操作,是否有一个回退不会破坏我的表单,而不是使用Javascript?当前,如果有一个散列(通常在地址字段中),那么之后的所有内容都不会被解析或存储在$_GET中。

4 个解决方案

#1


4  

You can encode the hash as you should urlencode(in php) or encodeURIComponent(in JavaScript).

您可以对散列进行编码,就像您应该在php中使用urlencode(在php中)或encodeURIComponent(在JavaScript中)一样。

The "hash" is not part of the request, which is why your script never sees it.

“散列”不是请求的一部分,这就是为什么您的脚本无法看到它。

#2


2  

Like webdestroya said, you'll need to send a request with the URL

像webdestroya说的那样,你需要发送一个带有URL的请求。

index.php?name=Ben&address=101%20S%2010th%20St%20Suite%20%23301

If you're using HTML forms, then the string value will be auto-urlencoded when you submit the form.

如果您使用的是HTML表单,那么当您提交表单时,字符串值将自动编码。

#3


1  

the user will be clicking a link from an email and will want to see the hash mark rendered in the email

用户将从电子邮件中点击一个链接,并希望看到在邮件中呈现的散列标记。

You need to encode the link to what Ben quoted before you stick it in the e-mail. What you currently have is not a URL at all.

您需要将链接编码到本引用的链接,然后在电子邮件中插入。您目前所拥有的不是一个URL。

You can optionally encode a space to + instead of %20 in the context of query parameters but you absolutely cannot include a raw space, because it is a defining characteristic of URLs that they don't have spaces in. If you type a space in a URL in a web browser it will quietly fix up the mistake, but an e-mail client can't pick out a URL from plain text if it's full of spaces.

在查询参数上下文中,您可以选择性地将一个空格编码为+,而不是%20,但是您绝对不能包含原始空间,因为这是url的定义特性,它们没有空格。如果您在web浏览器中的URL中输入一个空格,它将会悄悄地修复错误,但是如果一个电子邮件客户端充满了空间,则无法从纯文本中选择一个URL。

There is sometimes an alternative function which encodes spaces to + instead of %20. Normally this is best avoided as + isn't valid in all circumstances, but if prefer:

有时会有一个替代函数,将空格编码为+,而不是%20。通常情况下,这是最好避免的,因为+在任何情况下都是无效的,但如果喜欢:

index.php?name=Ben&address=101+S+10th+St+Suite+%23301

then you'd use PHP's urlencode function instead of the more standard rawurlencode.

然后您将使用PHP的urlencode函数而不是更标准的rawurlencode。

Either way, you must encode the hash to %23, because otherwise a hash in an HTTP URL means the fragment identifier (the part of the page to scroll the browser to). This is not part of the address of the page itself; it is not even passed from the browser to the server, so you certainly cannot retrieve it—from $_GET or any other interface.

无论哪种方式,都必须将散列编码为%23,否则,HTTP URL中的散列意味着片段标识符(该页面的一部分用于滚动浏览器)。这不是页面本身的地址;它甚至没有从浏览器传递到服务器,所以您当然不能从$_GET或任何其他接口检索它。

There are many other characters in a component like an address that must be %-encoded before being inserted into a URL string, or they'll leave you with an invalid or otherwise non-functional URL. If all that %23 business looks funny in a URL... well, you'll have to live with it. That's what URLs have always looked like.

在一个组件中有许多其他的字符,比如在插入URL字符串之前必须有%编码的地址,否则它们会给您留下无效的或非功能性的URL。如果所有的%23业务在URL中看起来很滑稽…好吧,你得忍受它。这就是url的样子。

#4


0  

I usually store the hash on a cookie onunload

我通常将散列存储在一个cookie onunload中。

ej:

ej:

window.unload = function(){

 if(document.location.hash) setCoockie('myhash',document.location.hash);

};

#1


4  

You can encode the hash as you should urlencode(in php) or encodeURIComponent(in JavaScript).

您可以对散列进行编码,就像您应该在php中使用urlencode(在php中)或encodeURIComponent(在JavaScript中)一样。

The "hash" is not part of the request, which is why your script never sees it.

“散列”不是请求的一部分,这就是为什么您的脚本无法看到它。

#2


2  

Like webdestroya said, you'll need to send a request with the URL

像webdestroya说的那样,你需要发送一个带有URL的请求。

index.php?name=Ben&address=101%20S%2010th%20St%20Suite%20%23301

If you're using HTML forms, then the string value will be auto-urlencoded when you submit the form.

如果您使用的是HTML表单,那么当您提交表单时,字符串值将自动编码。

#3


1  

the user will be clicking a link from an email and will want to see the hash mark rendered in the email

用户将从电子邮件中点击一个链接,并希望看到在邮件中呈现的散列标记。

You need to encode the link to what Ben quoted before you stick it in the e-mail. What you currently have is not a URL at all.

您需要将链接编码到本引用的链接,然后在电子邮件中插入。您目前所拥有的不是一个URL。

You can optionally encode a space to + instead of %20 in the context of query parameters but you absolutely cannot include a raw space, because it is a defining characteristic of URLs that they don't have spaces in. If you type a space in a URL in a web browser it will quietly fix up the mistake, but an e-mail client can't pick out a URL from plain text if it's full of spaces.

在查询参数上下文中,您可以选择性地将一个空格编码为+,而不是%20,但是您绝对不能包含原始空间,因为这是url的定义特性,它们没有空格。如果您在web浏览器中的URL中输入一个空格,它将会悄悄地修复错误,但是如果一个电子邮件客户端充满了空间,则无法从纯文本中选择一个URL。

There is sometimes an alternative function which encodes spaces to + instead of %20. Normally this is best avoided as + isn't valid in all circumstances, but if prefer:

有时会有一个替代函数,将空格编码为+,而不是%20。通常情况下,这是最好避免的,因为+在任何情况下都是无效的,但如果喜欢:

index.php?name=Ben&address=101+S+10th+St+Suite+%23301

then you'd use PHP's urlencode function instead of the more standard rawurlencode.

然后您将使用PHP的urlencode函数而不是更标准的rawurlencode。

Either way, you must encode the hash to %23, because otherwise a hash in an HTTP URL means the fragment identifier (the part of the page to scroll the browser to). This is not part of the address of the page itself; it is not even passed from the browser to the server, so you certainly cannot retrieve it—from $_GET or any other interface.

无论哪种方式,都必须将散列编码为%23,否则,HTTP URL中的散列意味着片段标识符(该页面的一部分用于滚动浏览器)。这不是页面本身的地址;它甚至没有从浏览器传递到服务器,所以您当然不能从$_GET或任何其他接口检索它。

There are many other characters in a component like an address that must be %-encoded before being inserted into a URL string, or they'll leave you with an invalid or otherwise non-functional URL. If all that %23 business looks funny in a URL... well, you'll have to live with it. That's what URLs have always looked like.

在一个组件中有许多其他的字符,比如在插入URL字符串之前必须有%编码的地址,否则它们会给您留下无效的或非功能性的URL。如果所有的%23业务在URL中看起来很滑稽…好吧,你得忍受它。这就是url的样子。

#4


0  

I usually store the hash on a cookie onunload

我通常将散列存储在一个cookie onunload中。

ej:

ej:

window.unload = function(){

 if(document.location.hash) setCoockie('myhash',document.location.hash);

};