What are fragment URLs and why to use them?
Asked Answered
I

4

41

I am new to PHP Development.

Today I came across the interesting topic of URL fragments, specifically the # part of URLs.

I searched and found that it's like

www.example.com/foo.html#bar

But I don't understand why this #bar is needed. Or how to read it in PHP?

Idou answered 23/6, 2015 at 8:18 Comment(2)
You don't read it in PHP, it's a javascript thing. The fragment is never sent to the server.Diophantus
PHP wants to be a general-purpose language, so its URL library does parse fragments. But PHP is most often used to respond to HTTP client requests, which will never contain fragments. So the question is a little confusing because the OP doesn't say whether he's writing webserver software. The answer rated -1 actually is correct for "how to read it in PHP" but is probably misleading. OP, please clarify whether you are writing webserver software?Alanis
W
66

A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page.

In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.

There are a few things about the fragments, the most important may be that they aren't sent in HTTP request messages but you can find some more info about them on this page.

Javascript can manipulate fragments on the current page which can be used to to add history entries for a page without forcing a complete reload.

Waffle answered 23/6, 2015 at 8:27 Comment(0)
T
1

It's unable to read it by php. It uses by client side (browser) for hash navigation, but you can write JS code to handle hash change and send async request to your server side (php) and display result on your page.

Togliatti answered 23/6, 2015 at 8:22 Comment(0)
S
0

For reading a fragment by the PHP you can use 'parse_url( $url, PHP_URL_FRAGMENT )' function. This function in a built-in PHP function. follow example can help you to understand how to use it:

$url = 'www.example.com/foo.html#bar';
echo '<pre>';
var_dump(parse_url($url, PHP_URL_FRAGMENT));
echo '</pre>';

the result is:

string(3) "bar"

for more information about parse_url you can read this page

Sophocles answered 2/9, 2019 at 15:12 Comment(2)
While parse_url works after you have a complete URL, PHP cannot obtain a complete URL, including the anchor name. Since anything JavaScript and the DOM can manipulate might need to be processed by PHP, it doesn't make sense that PHP cannot obtain the entire URL with which it is called. Seems to be a very big but in PHP, unless I'm missing something.Hollie
@DavidSpector I am not sure how this is a bug in PHP. Per definition, a fragment is not sent to the server. As PHP runs on the server, there is no way PHP could ever access the fragment of any URL from which it is called. Just the same as any other server-based website framework like .NET, Ruby, node, etc.Decretory
Q
0

A fragment URL, also known as an anchor link, is a URL that includes a fragment identifier, which is a part of the URL that points to a specific section within a web page. The fragment identifier starts with a # symbol followed by an identifier.

Example of a Fragment URL

https://www.example.com/documentation.html#installation
Quaternary answered 20/6 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.