简体   繁体   English

如何在PHP中回显上次访问的页面?

[英]How to echo the last visited page in PHP?

I don't know is there is a PHP function like the ones that start with $_SERVER[''] That tell user which page he came from, on his current page. 我不知道是否有PHP函数,例如以$ _SERVER ['']开头的函数,该函数告诉用户他当前页面来自哪个页面。

ex. 例如 If I was browsing foo.com?id=abc then went to foo.com?id=efg, I need the current page to tell me that I came directly from foo.com?id=abc 如果我正在浏览foo.com?id=abc,然后转到foo.com?id=efg,则需要当前页面告诉我我直接来自foo.com?id=abc

I need this code badly, so any help is appreciated. 我非常需要此代码,因此不胜感激。

It is $_SERVER['HTTP_REFERER'] . 它是$_SERVER['HTTP_REFERER'] But it is filled only if browser did so. 但是,只有浏览器这样做时,它才会被填充。 Otherwise you need to track user yourself (ie by storing last page in session) 否则,您需要自己跟踪用户(即通过存储会话中的最后一页)

The $_SERVER variables should not be relied upon to provide accurate answers. 不应依赖$ _SERVER变量来提供准确的答案。 You should use PHP Sessions to track what page they come from, and simply update it everytime they go to a new page. 您应该使用PHP会话来跟踪它们来自哪个页面,并在每次进入新页面时对其进行更新。 Something along the lines of: 类似于以下内容:

session_start();
if(!empty($_SESSION['visited_pages'])) {
  $_SESSION['visited_pages']['prev'] = $_SESSION['visited_pages']['current'];
}else {
  $_SESSION['visited_pages']['prev'] = 'No previous page';
}
$_SESSION['visited_pages']['current'] = $_SERVER['REQUEST_URI'];

Then to access the previous page, access the: $_SESSION['visited_pages']['prev'] 然后访问上一页,访问: $ _SESSION ['visited_pa​​ges'] ['prev']

是的,不仅在PHP中,而且这是HTTP协议规范的一部分,请使用:

$_SERVER['HTTP_REFERRER']

The HTTP_REFERRER gives address of the page that requested the file. HTTP_REFERRER提供请求文件的页面的地址。 For example an image on a page is a separate request, and this request has a $_SERVER['HTTP_REFERRER'] set to the page. 例如,页面上的图像是一个单独的请求,并且此请求将$ _SERVER ['HTTP_REFERRER']设置为页面。

I don't think browsers allow servers to access history. 我认为浏览器不允许服务器访问历史记录。 It can be done with JavaScript, though only a back button can be provided, the url cannot be accessed easily. 可以使用JavaScript完成此操作,尽管只能提供后退按钮,但无法轻松访问该url。 Though it can be achieved using a simple css and javascript trick by accessing the computed color to a link . 尽管可以使用简单的CSS和javascript技巧通过将计算出的颜色访问链接来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM