search

Home  >  Q&A  >  body text

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>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>
P粉145543872P粉145543872463 days ago464

reply all(2)I'll reply

  • P粉724256860

    P粉7242568602023-08-21 15:01:41

    Simple test, visit http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server

    python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
    Serving HTTP on 0.0.0.0 port 8000 ...
    localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
    localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -

    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:

    <script>alert(window.location.hash);</script>

    If you already have a URL string containing a fragment, the parse_url() function in PHP can be used (http://codepad.org/BDqjtXix):

    <?
    echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
    ?>
    
    输出:fizzbuzz

    But I don't think PHP will receive the fragment information since it only exists on the client side.

    reply
    0
  • P粉360266095

    P粉3602660952023-08-21 14:50:26

    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.

    reply
    0
  • Cancelreply