Retrieving Complete URL, Including Query String and Anchor
Getting the complete URL that was used to request the current page, including the anchor, can be a useful requirement when working with included pages. Consider a scenario where a page named "foo.php" is included within "bar.php." If you wish to know the complete URL in "foo.php," you need it to display "bar.php?blarg=a#example."
The Issue with Anchors
Unfortunately, the hash (the part of the URL after the #) is never sent to the server. It is exclusively used by the browser to control page behavior. As a result, the PHP variable $_SERVER['REQUEST_URI'] will contain everything except the anchor.
Using JavaScript for Anchors
If you absolutely need to know the anchor, you must use the JavaScript property "document.location.hash," which stores the anchor's content. You can retrieve the hash and then include it in a form or send it to the server via an AJAX request.
The above is the detailed content of How to Retrieve the Complete URL with Anchor in PHP?. For more information, please follow other related articles on the PHP Chinese website!