What's the best way to access a URI variable after a hashtag from php?

天涯浪子 提交于 2019-12-13 04:11:24

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/2160118/whats-the-best-way-to-access-a-uri-variable-after-a-hashtag-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!