简体   繁体   English

如何使用php检测上次访问的页面?

[英]How can I detect the last visited page with php?

I am on my page index.php. 我在页面index.php上。 From here I go with a link to myform.php. 从这里开始,我带有指向myform.php的链接。

myform.php: myform.php:

 <?php 
       echo "My last visited page is:".$_SERVER['HTTP_REFERER'];
   ?>

<form action="success.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>

The result I see on my page is: 我在页面上看到的结果是:

My last visited page is http://www.mypage.com/index.php

Now I submit my form and come to success.php. 现在,我提交我的表格并进入success.php。 Here I click my "back" button and come again to myform.php. 在这里,我单击“后退”按钮,然后再次访问myform.php。 The result I see is now: 我现在看到的结果是:

  My last visited page is http://www.mypage.com/index.php

But what I expect here is: 但是我在这里期望的是:

My last visited page is http://www.mypage.com/success.php

You can use sessions.. 您可以使用会话。

Bottom of the success.php you can assign page name to sessions 您可以在success.php的底部为会话分配页面名称

$_SESSION['page'] = $_SERVER['HTTP_REFERER'];

When click on back button you can get that value using sessions(index.php), 点击返回按钮时,您可以使用session(index.php)获得该值,

echo $_SESSION['page'];

On that page at the bottom, you can assign current page name to session, then it can track on next page 在底部的该页面上,您可以为会话分配当前页面名称,然后可以在下一页上进行跟踪

The back button does strange things with variables and page refreshing. 后退按钮通过变量和页面刷新来完成一些奇怪的事情。 Try typing the http://www.mypage.com/myform.php address into the browser and seeing what happens. 尝试在浏览器中输入http://www.mypage.com/myform.php地址,看看会发生什么。

NB I know this only deserves to be a comment, but reputation is a pain. 注意:我知道这仅是一个评论,但是声誉是痛苦的。

As you need to carry the previous info to forwarded page. 因为您需要将以前的信息携带到转发页面。 Then it will be better if you use session. 然后,如果您使用会话会更好。 When you are visiting a page, store this current page in a session variable. 当您访问页面时,请将当前页面存储在会话变量中。 Add this line at bottom on each page. 在每一页的底部添加此行。

$_SESSION['page'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

And to echo 并呼应

<?php 
   echo "My last visited page is:".$_SESSION['page'];
?>

So whole text will look like: 因此全文如下所示:

<?php 
       echo "My last visited page is:".$_SESSION['page'];
?>

<form action="success.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
<?php
     $_SESSION['page'] ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>

edit: Be careful when you are using multiple tab. 编辑: 使用多个选项卡时要小心。 Only latest info will be stored. 仅会存储最新信息。

One way is, you can handle this by using PHP's _SERVER global, If the user first time visits your site, there will be no previous page, except that, the previous page will be last visited page identified by _SERVER , check out this script, 一种方法是,您可以使用PHP的_SERVER全局_SERVER来处理此问题,如果用户首次访问您的网站,将没有前一页,除了前一页将是_SERVER标识的最后访问的页面之外,请查看此脚本,

isset($_SESSION['current']) || $_SESSION['current'] = '';
// check for first visit to any page, initialize

if($_SERVER['SCRIPT_NAME'] != $_SESSION['current']){
// check if current page != previously recorded page    

    // order is important here in two lines, check it yourself
    $_SESSION['previous'] = isset($_SESSION['current']) ? $_SESSION['current'] : '';
    $_SESSION['current'] = $_SERVER['SCRIPT_NAME'];

}

You can make something like this, which ignores same page if user refreshes the page, disregarding the method being GET or POST. 您可以创建类似这样的内容,如果用户刷新页面,则忽略相同的页面,而不管方法是GET还是POST。

You can include this script in common file to record the activity. 您可以将此脚本包含在公共文件中以记录活动。

Array
(
    [current] => /session/index.php
    [previous] => /session/submit.php
)

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

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