在URL末尾的#text时,$ _SERVER ['REQUEST_URI']不起作用

I know $_SERVER['REQUEST_URI'] works in getting the current page's url, but it leaves out the #post at the end of the url. I like to use the #blah at the end to make the page scroll to a certain point. Example:

index.php#answer

using $_SERVER['REQUEST_URI'] just returns:

index.php

How can I make it read the #answer at the end as well?

URL hashes are never sent to the server. So it's impossible to retrieve it using PHP or any other server-side language.

To achieve the scrolling, give the element you want to scroll to id="answer" - then the browser will jump to it automatically. In case you want to perform smooth scrolling using JavaScript, you can access the hash via location.hash - there are some nice jQuery plugins available which will take care of scrolling smoothly to a specified element.

That won't work since hashes won't be sent to the server it is for on-page anchoring.

You can only use the PATH_INFO and the QUERY_STRING or the REQUEST_URI which is the combination of the two mentioned before. QUERY_STRING is the part after an optional ? in the URI.

You can access that hash from javascript and send it through AJAX if you really want to.