答案是肯定的和否定的
如果有人非常准确地阅读了您的问题,那么您实际上提出了两个不同的问题:
点击返回按钮时更改之前显示页面的内容
答案: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
}