Home > Backend Development > PHP Tutorial > How to Retrieve the Complete URL with Anchor in PHP?

How to Retrieve the Complete URL with Anchor in PHP?

Susan Sarandon
Release: 2024-10-18 10:59:02
Original
341 people have browsed it

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.

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template