【问题标题】:Change content of previously displayed page when hitting back button点击返回按钮时更改先前显示页面的内容
【发布时间】:2018-02-11 11:39:55
【问题描述】:

有没有办法只通过使用 PHP(无 AJAX)来更改用户通过单击返回按钮返回的网页?

为了说明,让我们假设这个场景:我有一个用 PHP 编写的网站,有一个主页和一个“联系我们”页面。 “联系我们”页面的 PHP 脚本生成联系表单并显示它,它还通过将表单提交给自己来处理它。申请流程是这样的:

第 1 步:首页(用户点击“联系我们”)

第 2 步:联系表单页面(用户填写表单并点击提交)

第 3 步:联系表单页面(脚本处理用户输入并显示成功消息)。

如果用户点击浏览器中的后退按钮(即避免再次显示联系表单),我想做的是引导已到达第 3 步的用户返回第 1 步,但仅使用 PHP,不使用 AJAX 或Javascript 什么的。这可能吗?

【问题讨论】:

  • 这应该没什么大不了的。您可以自行存储历史记录。您可以设置 cookie、创建静态文件、存储到数据库、php 会话处理,也许更多......

标签: php


【解决方案1】:

答案是肯定的和否定的

如果有人非常准确地阅读了您的问题,那么您实际上提出了两个不同的问题:

点击返回按钮时更改之前显示页面的内容

答案:Jepp!其实这没什么大不了的。

有没有办法只通过使用 PHP(无 AJAX)来更改用户通过单击返回按钮返回的网页?

答案:不!它始终是浏览器历史日志堆栈中当前页面之前的页面。但如上所述,您绝对可以更改内容本页以上一页为准。

示例: 假设一个在站点“home.php”上,导航到“contact.php”,然后点击返回按钮。他肯定会再次回到站点“home.php”(至少只使用 PHP)。但是“home.php”上的 PHP 脚本可以知道哪个是前一页并提供完全不同的视图。与此无关,浏览器地址栏中的 url 将保持为“home.php”,显示的内容由您决定。

要避免这种情况,您只能进行重定向以实际更改位置。


让我们从基础开始

如果您想根据前一页做出决定,您肯定必须知道用户访问的是前一页 - 对吧?
当然,不仅有一种解决方案,但对于您描述的设置,我建议使用PHP session。实际上,如果您想重定向您的观众,您只需要 session_start() function$_SESSION superglobal 以及 header() function

也许你想看看我为引导场景制作的模型。

这只是一个文件,其行为方式完全符合您的要求(“如果用户点击浏览器中的后退按钮,则将已到达第 3 步的用户引导回第 1 步”)。 它应该是开箱即用的 - 所以请随意在服务器上对其进行测试。

<?php

session_start();

// get and sanitize page id
$view = key( $_GET );
if ( !$view || 'home' != $view && 'contact_us' != $view && 'thank_you' != $view ) {
    $view = 'home';
}

// get last page id and set current page as the new last page
$last_view = $_SESSION['last_view'];
$_SESSION['last_view'] = $view;

// VALIDATE VIEW
// redirect or manipulate the assumed page under specific circumstances
if ( 'thank_you' == $last_view ||
     'thank_you' == $view && 'contact_us' != $last_view ) {
    // redirect version
    header('Location: /?home');
    /* change assumed page version (just uncomment the line below
     * and delete the one above to see the different behavior)
     * the delivered content will be the same in this case but the adress bar
     * will not change accordingly
     */
    //$view = 'home';
}

?><!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><?php echo $view; ?></title>
</head>
<body>

<nav>
    <?php echo 'home' == $view
        ? '<a href="/?contact_us">Contact Us</a>'
        : '<a href="/?home">Home</a>'; ?>
</nav>

<h1>last page was "<?php echo $last_view; ?>"
    and current one is "<?php echo $view; ?>"</h1>

<?php if ( 'home' == $view ) : ?>
    <!-- content home or content when last page was thank you -->
    <h2>Welcome Home</h2>

<?php elseif ( 'contact_us' == $view ) : ?>
    <!-- content contact_us -->
    <h2>Contact Us</h2>
    <form action="?thank_you" method="POST">
        <input name="name" value="" placeholder="Whats your name?" required>
        <input type="submit">
    </form>

<?php else : ?>
    <!-- content thank_you -->
    <h2>Thank You So Much "<?php echo $_POST['name']; ?>"</h2>

<?php endif;

这是一个更适合您当前设置的版本,具体取决于如何获取页面 id

<?php
session_start();

// get your page id by filename in conjuction with $_POST data
// if your form at site "contact_us.php" uses `method="get"` just change "$_POST" to "$_GET"
$view = $_POST ? 'thank_you' : pathinfo( $_SERVER['PHP_SELF'], PATHINFO_FILENAME );

$last_view = $_SESSION['last_view'];
$_SESSION['last_view'] = $view;

if ( 'thank_you' == $last_view ||
     'thank_you' == $view && 'contact_us' != $last_view ) {
    header('Location: /'); // will redirect to the root of your domain
    //$view = 'home';
}
?><!DOCTYPE html>

那么现在一切都好吗?

不 - 恕我直言!

除了蜜蜂在页面上点击后退按钮之外还有更多。例如,重新加载按钮怎么样。或者如何非常快速地多次点击后退按钮或所说的不同组合。如果您尝试考虑每一种甚至很多可能的情况,我敢打赌,您可能会头疼!那只是非常 hacky - 你知道 :]

再次声明:您可以将我的脚本作为起点,如果它真的只是点击后退按钮,那么它会为您完成。


但是更好的解决方案呢?

推荐

在一个页面中完成并使用 AJAX。 不是一般情况,而是在您所描述的场景中!

其实这样会更简单直接!


如果你真的只想继续使用 PHP,那么这里有一个可能

更好的方法

当用户提交表单时,可以捕获当前时间戳,添加一些额外时间(“联系我们”将重定向到主页的阶段)并将此时间戳部署为 $_SESSION 变量。

<?php
session_start();

// redirect to home page if in restricted period and not on home page
if ( $_SESSION['restricted_period'] &&
     $_SESSION['restricted_period'] > $_SERVER['REQUEST_TIME'] ) {
    if ( 'contact_us.php' == pathinfo( $_SERVER['PHP_SELF'], PATHINFO_BASENAME ) ) {
    // if ( 'contact_us' == key( $_GET ) ) { // that would be suitable for the mockup above
        header('Location: /'); // will redirect to the root of your domain
    }
} else {
    $_SESSION['restricted_period'] = null;
}

// if your form at site "contact_us.php" uses `method="get"` just change "$_POST" to "$_GET"
if ( $_POST ) { // the "thank you" part
    // means the visitor will not be able to visit "contact us" for 45 seconds from now on
    $_SESSION['restricted_period'] = strtotime('+45 seconds'); // short for testing
    // change to '+10 minutes', '+1 hour', whatever you like
}

【讨论】:

  • 感谢您的回答,+1 表示详尽。
  • 不客气!顺便说一句(只是好奇):你现在的策略是什么?为什么不想使用 ajax?
  • 其实是为了确认我走对了路。我在 PHP 中处于中级到高级,但对 ajax 完全一无所知(至少直到几天前)。我认为如果我想解决问题中提到的功能,我需要学习如何使用它,所以我遇到了麻烦,但我想确认没有其他明智的方法。所以感谢您的回复,这让我看到 ajax 确实是明智的选择!
【解决方案2】:

我认为您可以在 PHP 文件中使用 header() 指令。不幸的是,这不适用于浏览器按钮。此代码将如下所示:

直接提交到 script.php 文件时的表单。

<form action="script.php" method="post">
     //form controls
</form>

script.php 文件。

<?php
   //Do your stuff with data from user
   header("Location: index.php"); //this send to first page
?>

此外,您可以向首页发送一些 GET 参数以更改内容或执行其他操作,例如:

<?php
   //Do your stuff with data from user
   header("Location: index.php?action=dosomething");
?>

您必须记住,标头必须是从 script.php 文件输出的第一个数据。

希望我的回答有所帮助。

干杯

【讨论】:

    【解决方案3】:

    在 PHP 中无法控制浏览器事件,例如后退或前进按钮,因为 PHP 是在服务器端而不是在客户端处理的。所以你不能在 PHP 中做到这一点。 但是您可以在 JavaScript 中使用 HISTORY API 使用户重定向到第 1 步。 您可能会在这里获得更多帮助 Back button redirect script

    PS:改变浏览器行为一般是不行的。

    我希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      你不能,但你可以使用一个技巧,你可以在第 3 步创建一个会话 var,然后当用户使用后退按钮是或是时将转到第 2 步,但你可以设置一个条件是 var session 存在转到第 1 步进行重定向

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-13
        相关资源
        最近更新 更多