tags:

views:

5626

answers:

5

Assuming a URL of:

  www.mysite.com?val=1#part2

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

Is the hash value "part2" also readable? or is this only upto the browser and JavaScript.

+1  A: 

The hash is never sent to the server, so no.

PatrikAkerstrand
+19  A: 

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.

Ionuț G. Stan
+2  A: 

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.

Silfverstrom
+5  A: 

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.

Alister Bulman
+1 This is my way too.
Pawka
+11  A: 

Simple test, accessing 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 server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.

You can find the anchor name used within the URL via javascript using, as an example:

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

The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):

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

Output: fizzbuzz

But I don't think PHP receives the fragment information because it's client-only.

tom