1

When using the $_GET[] in php, it searches for a variable e.g ?id=value

Is there a way to call the #value instead?

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
Andy
  • 2,991
  • 9
  • 43
  • 62

4 Answers4

8

No, because the hash part of the url is client-side only and is not sent to the server.

When you enter an URL such as http://server.com/dir/file.php?a=1#something in your browser's URL textbox, the browser opens a connection to the server.com and then issues a HTTP command GET /dir/file.php?a=1 HTTP/1.1. This is the only data sent to the server. Hence, the server never gets the #something part, and this means there is no script on server side you could write to read that value.

Similar question explained here: How to get Url Hash (#) from server side

Community
  • 1
  • 1
naivists
  • 32,681
  • 5
  • 61
  • 85
  • Thanks for your prompt answer naivists – Andy Jan 07 '10 at 19:24
  • +1, absolutely true. However, here's an example that makes things appear thoroughly confusing: http://www.google.com/#q=test . Google appears to bend the rules thanks to some clever javascript. If you clear your cache and watch LiveHTTPHeaders, a request for / is sent (without the fragment), which loads javascript, which triggers a request for the actual search result data (the query string for this request is read from the fragment in Window.location(?) using javascript): GET /search?q=test&fp=1&cad=b HTTP/1.1. I'm sure there's some interesting reason google does things this way. – Frank Farmer Jan 07 '10 at 21:53
  • @naivist Interesting. I was convinced having seen the fragment in an URL on the serverside before, but you are right, thus I deleted my answer. Now I only need to find the RFC that says why it is like this. After all, it's a valid URI component. – Gordon Jan 07 '10 at 22:25
  • Frank Farmer, one of reasons why this may be done is - to let the client stay on the same page without refresh. That is, if you add a link in your page to, say, `samepage.htm#something`, and click that link, your page is not refreshed. However, you can catch that event in javascript, and, using AJAX, call for some more data from the server. – naivists Jan 08 '10 at 05:02
0

I've been able to work around it by getting the fragment via javascript and sending an ajax request with the fragment as the $_GET contents.

gabrielk
  • 573
  • 6
  • 11
0

Without knowing your whole case, I may be off track, but there is the possibility of sending the #something to the server via a simple GET type of xmlhttprequest.

luminarious
  • 177
  • 2
  • 12
-4

Yeah there is a way. I think what you want to do is:

$arValues = array_values($_GET);
// whatever else you want to do with the values
Daniel
  • 75
  • 1
  • 5
  • He wants to get the URL fragment. What he means by ?= is the regular params, e.g. foo=bar. But he is looking for the value in the URL after #. – Gordon Jan 07 '10 at 19:52