dongtan6695 2012-04-25 14:48
浏览 56
已采纳

用户按下后退按钮时不重绘页面(在PHP中使用$ _GET变量)

I have a dynamic website with pages drawn using PHP.

Using the $_GET variable I am getting commands from the user, executing PHP based on the $_GET command, and drawing a page.

However, when the user presses the back button I want them to see the page that was dynamically drawn for them before, instead of re-executing code.

I've seen this done, but can't figure out how to do it.

For instance assume the following code:

if ($_GET['cmd'] == "time") {
   echo "The current Unix timestamp is: " . time;
}

Clicking the url: somepage.php?cmd=time executes the code properly but when using the back button, re-executes the code. Is there a way using cache, or something else I don't know about, that will allow the user to see the time as it was when the page was drawn, instead of re-executing?

========================================================================================= To try and be a little more specific, the pages and code that I am talking about perform multiple functions and alter MySQL data based on the commands given then draw the page.

I want to know if there is a way, when using the back button, to not re-execute but to just show the page that was drawn dynamically the first time.

  • 写回答

3条回答 默认 最新

  • dozr13344 2012-04-25 14:53
    关注

    I' not sure if this is what you are looking for, but it may be of help. Rather than using php get, you might use ajax and then implement this: http://www.nerdswithlives.com/2010/03/yui-ajax-browser-history-back-button.html

    You could also have some type of variable stored every time a specific get command is executed, then check for that variable to determine which content to redraw on page load.

    EDIT

    From thinking more about your problem, I believe the answer lies with using PHP Sessions, and storing data on the clients machine. When a user clicks "back" he/she IS going to the cached page... so caching is not your answer. You need it re-drawn a specific way, but because you are using GET, the browser does NOT cache this... at least with back button functionality anyway. Your answer is to start a session on each page this dynamic content exists, store a variable like $_SESSION['sessionVar'] = 1; or whatever. Then dynamically change the variable depending on what was drawn on that page. Then, when the user clicks "back" you can check for whatever that variable is and get the data again. Get out of the mindset of using cache for this - you need to RE-DRAW whatever data the user saw previously. Sessions would be useful in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭