PHP & Hash/Fragment Portion of Url

What are fragment URLs and why to use them?

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.

Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.

Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.

Get fragment (value after hash '#') from a URL

If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"] or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.

If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.

Why is the hash part of the URL not available on the server side?

No, it is available to the browser only, so you have to deal it with Javascript. The server can not read it.

Explanation:
Basically the hash component of the page URL (the part following the # sign) is processed by the browser only - the browser never passes it to the server. This sadly is part of the HTML standard and is the same whether or not you are using IE or any other browser (and for that matter PHP or any other server side technology).

Here's what Wikipedia says about it:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the fragment value. In the most common case, the agent scrolls a Web page down to the anchor element which has an attribute string equal to the fragment value. Other client behaviors are possible

Is the URL fragment identifier sent to the server?

Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.



Related Topics



Leave a reply



Submit