views:

291

answers:

1

I have javascript updating my URI as below:

/index.php?page=list#page=news

But I would like to make page=news accessible somehow from my server so that when the URI is copied and pasted, it would go to the news page. What is the best way to do this ?

I tried $_SERVER['REQUEST_URI'] but everything stored in $_SERVER is before the hash tag.

Thanks.

+3  A: 

You can't. That data, called the fragment, is reserved for client side processing and thus is never sent to the server.

The only way to utilize the fragment is to have Javascript intervene at some point. This probably means checking for a hash-tag on the page onload, and then displaying the proper data.

You could make your entire page loaded via Javascript. While it would kill compatability for anyone who turned off Javascript, it would ensure that the hash tag eventually gets sent to PHP

Basically, it would look something like this:

  • PHP Sends Page
  • Javascript reads the hastag
  • Make a URL with a hashtag parameter (loader.php?page=list&page=news)
    (Note that in the above, page=list wil be overriden by page=news, so $_GET['page'] will be news.
  • AJAX call to PHP
  • Load the content into a div.

(And this question is very much a duplicate question)

Chacha102
Thank you for the fast response. Would this also be possible with a javascript client redirection? But how would I get it to ignore the inclusion of hashtags from the page navigation to prevent an infinite loop?
ensnare
What I suggested is very much a 'Javascript Application' way of doing it, where you only keep a single page open, and change it with AJAX loads. You could make something that in Javascript that check if the `page` GET variable was the same as the hashtag `page` variable, it wouldn't redirect.
Chacha102