在没有 JavaScript 的 PHP 中模拟后退按钮
我需要一个“后退”按钮,但不使用 JavaScript。我已经想到了一种可能的方法:
使用会话变量(例如 $_SESSION['http_referer']
),该变量每次加载时都会在每个页面上更新,
将其内容保存到变量(例如
$lastPage
)(供进一步使用 在当前页面中)为其分配
$_SERVER['REQUEST_URI']
但我不确定这有多有效(或低效)。至少是正确的吗?
I need a "back" button but without the use of Javascript. I already thought of one possible approach:
Using a session variable (e.g. $_SESSION['http_referer']
) which would be updated every time on every page whenever it's loaded,
saving it's content to a variable (e.g.
$lastPage
) (for further use
in the current page)assigning to it the value of
$_SERVER['REQUEST_URI']
But I'm not sure how efficient (or inefficient) this is. Is it at least correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户只能返回您的网站,那么最好在您存储在会话中的堆栈中跟踪他们访问过的页面,而不是
_SERVER[HTTP_REFERER]
。当用户单击“后退”按钮时,可以重定向到堆栈顶部的页面(页面加载完成后会添加到堆栈中,因此“后退”按钮应该使用上一页)。请注意,这与真正的后退按钮完全不同。相反,它会作为新页面添加到真实历史中。另请注意,在脚本执行期间写入
_SERVER[REQUEST_URI]
不会执行任何操作。If the user can only go back on your site it's much better to keep track of which pages they've visited in a stack you store in the session instead of
_SERVER[HTTP_REFERER]
. When the user clicks the "back" button, you can redirect to the page at the top of the stack (a page is added to the stack after it finishes loading, so the "back" button should use the previous page). Note that this is not the same as a real back button at all. Instead it is added to the real history as a new page.Also note that writing to
_SERVER[REQUEST_URI]
during script execution does nothing.