8

I am trying to find a way to save the hash portion of a url and as a PHP variable. This idea is a bit kooky, but bear with me...

I'd like to extract the "location" fragment from the following URL and save it as a PHP variable.

http://www.example.com/#location

However, discussion at this link indicates that the fragment of a URL is only reachable through JavaScript.

But would it be possible to create a link where the fragment is duplicated in the URL, parsed by PHP, and then removed by mod rewrite? So....

Original url:

http://www.example.com/location/#location

PHP gets location variable thanks to the plain "location" in the URL

Apache then rewrites the link to:

http://www.example.com/#location

I'm curious to know if there is an elegant way to solve this problem.

asked Jul 21, 2009 at 22:06
3
  • 2
    You could change the fragment into a $_GET value? Commented Jul 21, 2009 at 22:08
  • Nope, you can't. See my Answer Commented Jul 21, 2009 at 22:15
  • Here is a solution using javascript and a hidden input field stackoverflow.com/questions/940905/… Commented Mar 30, 2017 at 13:27

5 Answers 5

9

You'll need to use Javascript to read this. There are a few different options - upon page load, you could use an XmlHTTPRequest (AJAX request) to tell the server what the additional URL parameters were. Alternatley you could check to see if there are additional parameters (also via Javascript), and if you find any, post back to a different URL that has these parameters encoded into the URL itself.

answered Jul 21, 2009 at 22:16
5

The Fragment is never sent to the server, according to this thread on the Mod_Rewrite forums. So, this might be impossible unless you use AJAX to change the page after the fact.

Another idea would be to have Javascript turn the hash into a $_GET paramater, and then refresh the page.

answered Jul 21, 2009 at 22:14
4
  • Thanks for the replies! I know the fragment is never sent to the server. What I'm proposing is a "pseudo fragment" or "double fragment" like example.com/fragment/#fragment The first fragment would be interpreted by PHP, and then removed by mod_rewrite, rendering the URL as example.com/#fragment Maybe this approach makes no sense; I'm not sure if it does, hence my question... Commented Jul 21, 2009 at 23:09
  • If you want to send the person back to example.com/#fragment, you would send them to example.com/fragement and then PHP could forward them to example.com/#fragement. Commented Jul 22, 2009 at 2:17
  • Great idea to forward the user from "example.com/fragment" to "example.com/#fragment". I will use a cookie to save the fragment, then forward the page to itself. Something like this might work: Example: link: example.com/test.php?fragment=turkey $fragment = $_GET['fragment']; if (isset($fragment)) { setcookie("TestCookie", "TestCookie", time()+3600); header("Location: example.com/test.php/#$fragment"); } // do additional stuff based on cookie Commented Jul 22, 2009 at 4:57
  • You would end up with 2 entries in the history stack :( And worse, the first always refers to the second so your back button navigation would be bothersome. Commented May 7, 2011 at 16:12
0

you could send hash fragment via AJAX to PHP script and do an immediate refresh (reload of the page)

answered Jul 21, 2009 at 22:14
0
0

Once you have send the values to server via AJAX. You can set the fragment values in SESSION. When you refresh the page, you can get the fragment which was set in session and process then display the corresponding content. Because we can't get get the fragment values through PHP_SELF / QUERY_STRING and etc. We need this to increase the speed of our web page like Gmail.

answered Aug 8, 2010 at 13:00
-2

It's contained in the "fragment" value returned from PHP's parse_url function.

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);
?>

Will return:

Array
(
 [scheme] => http
 [host] => hostname
 [user] => username
 [pass] => password
 [path] => /path
 [query] => arg=value
 [fragment] => anchor
)
/path
answered Oct 26, 2011 at 5:43
1
  • 3
    This is true if the fragment is manually supplied to this function, but the problem in relation to the original question is that the fragment part is not sent to the server. Commented Jun 17, 2012 at 13:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.