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

前端 未结 12 2486
无人及你
无人及你 2020-11-21 04:37

Assuming a URL of:

www.example.com/?val=1#part2

PHP can read the request variables val1 using the GET array.

Is the ha

相关标签:
12条回答
  • 2020-11-21 04:47

    It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.

    0 讨论(0)
  • 2020-11-21 04:48

    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.

    0 讨论(0)
  • 2020-11-21 04:49
    <?php
    $url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
    echo $url["fragment"]; //This variable contains the fragment
    ?>
    

    This is should work

    0 讨论(0)
  • 2020-11-21 04:53

    I think the hash-value is only used client-side, so you can't get it with php.

    you could redirect it with javascript to php though.

    0 讨论(0)
  • 2020-11-21 04:59

    Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).

    On the client side, this can be accessed using javaScript with window.location.hash.

    0 讨论(0)
  • 2020-11-21 05:01

    Another solution is to add a hidden input field to the php page:

    <input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
    

    Using javascript/jQuery you can set the value of this field on the page load or responding to an event :

    $('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
    

    In php on the server side you can read this value using the $_POST collection:

    $server_location_hash = $_POST['myHiddenLocationHash'];
    
    0 讨论(0)
提交回复
热议问题