Is it possible to access anchors in a querystring via PHP?

无人久伴 提交于 2019-12-04 04:14:35

问题


I have a page that is accessed via a URL like this:

http://power-coder.net/Test/something.php?id=3#Page1

I know how to access the id parameter using $_GET, however is there a way for me to access the #Page1 part? I have looked at the $_SERVER array and the REQUEST_URI ends at ?id=3.

I know that I could also change the #Page1 to be an additional parameter like &Page=1, however there is a fair bit of code using the old URL type that I would like to avoid re-writing if at all possible.

Thanks for the help.


回答1:


No, there isn't a way. The fragment part (the label after #) is not transmitted to the server.

The browser retrieve the document http://power-coder.net/Test/something.php?id=3 and then go to the correct anchor (if there is one) in the document.




回答2:


The relevant text from the specs:

4.1. Fragment Identifier

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.




回答3:


In case someone is still searching for a solution.

Just put '&' before the anchor tag:

http://example.com/Test/something.php?id=3&#Page1

Tested on Chrome, Firefox, Safari and IE11



来源:https://stackoverflow.com/questions/484113/is-it-possible-to-access-anchors-in-a-querystring-via-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!