The title can be rewritten as: In a server-side application (like PHP, Ruby, Python, etc.), is it possible to read the hashed part of a URL?
P粉145543872
2023-08-20 20:10:37
<p>Suppose the URL is: </p>
<pre class="brush:php;toolbar:false;">www.example.com/?val=1#part2</pre>
<p>PHP can use the GET array to read the request variable <code>val1</code>. </p>
<p>Is the hash value<code>part2</code> also readable? Or can it only be handled by the browser and JavaScript? </p>
Simple test, visit http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
The request received by the server does not contain what follows the # symbol - what follows the # symbol is just an anchor lookup on the client.
You can use javascript to find the anchor name used in the URL, for example:
If you already have a URL string containing a fragment, the parse_url() function in PHP can be used (http://codepad.org/BDqjtXix):
But I don't think PHP will receive the fragment information since it only exists on the client side.
The main problem is that the browser won't even send the request with the fragment part. Fragment parts are parsed directly in the browser. Therefore, it is accessible via JavaScript.
Anyway, you can use parse_url() to parse the URL into its parts, including the fragment part, but obviously that's not your case.