"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Retrieve the Complete URL with Anchor in PHP?

How to Retrieve the Complete URL with Anchor in PHP?

Published on 2024-11-08
Browse:244

How to Retrieve the Complete URL with Anchor in PHP?

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.

Release Statement This article is reprinted at: 1729215316 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3