retrieve the hash in the url with php?

后端 未结 5 2123
说谎
说谎 2020-12-10 17:14

someone here in SO told me that the hash (#) in the url could be retrieved by php function parse_url? is this true?

my web site got a lot of ajax effects and i want

相关标签:
5条回答
  • 2020-12-10 17:36

    The hash in the URL doesn't even get to the server. There is no way you can access it from any programming language on the server side.

    0 讨论(0)
  • 2020-12-10 17:38

    Example from php manual:

    <?php
    $url = 'http://username:password@hostname/path?arg=value#anchor';
    
    print_r(parse_url($url));
    
    echo parse_url($url, PHP_URL_PATH);
    ?> 
    
    0 讨论(0)
  • 2020-12-10 17:42

    parse_url() can get the hash from a url string. Note the signature:

    mixed parse_url ( string $url [, int $component = -1 ] )
    

    You must already know the string. For instance, from the docs:

    <?php
    
      $url = 'http://username:password@hostname/path?arg=value#anchor';
      print_r(parse_url($url));
      echo parse_url($url, PHP_URL_PATH);
    
    ?>
    

    Outputs

    Array
    (
        [scheme] => http
        [host] => hostname
        [user] => username
        [pass] => password
        [path] => /path
        [query] => arg=value
        [fragment] => anchor
    )
    /path
    

    Note the entry under key "fragment".

    0 讨论(0)
  • 2020-12-10 17:46

    Once you get #value using javascript, send it back to server using ajax or .... url : http://example.in/?paramvalue=PKDVS4G#access_token=463d3d40-bdbb-04f3-ddb2-c35e2bd9ffa8

    <script>     
    alert(window.location.hash);
    var myhashvalue = window.location.hash;
    //hash value like :  #access_token=463d3d40-bdbb-04f3-ddb2-c35e2bd9ffa8
    //ajax call to send myhashvalue to server
    </script>   
    
    0 讨论(0)
  • 2020-12-10 17:54

    someone here in SO told me that the hash (#) in the url could be retrieved by php function parse_url? is this true?

    Yes:

    parse_url('http://stackoverflow.com/questions/1957030/retrieve-the-hash-in-the-url-with-php/1957040#1957040', PHP_URL_FRAGMENT); // 1957040
    

    But you can't determine the hash on the server side without knowing the full URL à priori, not sure if the HTTP_REFERER holds this hash (think not).

    0 讨论(0)
提交回复
热议问题