retrieve the hash in the url with php?
Asked Answered
N

5

6

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 to be able to pass an url with a hash to the index.php so it could display the information based on the hash values.

eg. if a user clicks and navigates in my website and he found a thing he wants to share, then he could copy the address and send it to his friend and when this person enters he sees the same thing.

i have tried using javascript to save the hash values in cookies but that isnt working with firefox and its too slow in safari (the html will show first, so one has to refresh the page a second time for the correct content to be shown).

any other solution is welcome

EDIT: im very confused..if i use the parse_url i have to type the url myself in php to get the hash? what is the point then...my question if whether my pal can enter the url with # values and php could process it? it seems like a no, am i right?

Natheless answered 24/12, 2009 at 6:31 Comment(3)
Some example code of what you mean would be helpful.Trunk
PHP can process it if you send the string to the server. You can do this via ajax, a form post, etc.Grabowski
but not thorugh the webbrowser when the user enter it right?Natheless
G
7

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".

Grabowski answered 24/12, 2009 at 6:33 Comment(3)
Not necessarily. You can pass the string to the server, and then use parse_url.Grabowski
You can access that hash value in javascript with window.location.hashGrabowski
by the way the above works only if you have a url saved as a string along with the hash, otherwise you will never be able to parse a # from url as they are never sent to the server. it's reachable through JavaScript thoZemstvo
D
12

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.

Deepset answered 24/12, 2009 at 6:32 Comment(0)
G
7

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".

Grabowski answered 24/12, 2009 at 6:33 Comment(3)
Not necessarily. You can pass the string to the server, and then use parse_url.Grabowski
You can access that hash value in javascript with window.location.hashGrabowski
by the way the above works only if you have a url saved as a string along with the hash, otherwise you will never be able to parse a # from url as they are never sent to the server. it's reachable through JavaScript thoZemstvo
R
0

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('https://mcmap.net/q/175135/-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).

Redpoll answered 24/12, 2009 at 6:34 Comment(0)
C
0

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);
?> 
Cue answered 24/12, 2009 at 6:35 Comment(0)
S
0

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>   
Shillelagh answered 6/5, 2017 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.