开发者

deal with undesirable behaviour when clicking the back button on my website

I've looked around for an answer to this problem, and I'm slowly accepting the fact that I've probably gone about things the wrong way, but here's a request for some wonderful pearl of wisdom that might help!

I'm working on a website which has a directory-style navigation bar on the left. So at first the user will only see two directories (Grammar / vocabulary - it's an English learning site) and by clicking on the directory, it will "open" to reveal its contents (other pages or directories).

The way I have this working is that each directory is a form submit button that reloads the page when clicked, but with a value in $_POST that tells the site to open or close a folder (this is done within an array in $_SESSION). It works great! The issue, though, is that if a user clicks the back button in their browser, they get the prompt asking if they wish to resubmit data, which is not something I want.

I was wondering if there was a way I can detect in php if the back button has been pressed, so that instead of reloading the page and making changes to the side bar, the side 开发者_高级运维bar will remain the same and instead the user gets taken to the last page they had visited.

I'm aware that the real solution to this is probably just to rethink the side bar, and any advice on a better way to do that would be much appreciated also!

Michael


The reason you're getting the prompt is because POST is supposed to be used if the data being sent is going to modify something, so submitting a POST request using the back button might have unexpected behaviour (like duplicate data).

Replacing POST with GET should disable the prompt.

HTML
From:

<form action="page.php" method="post">
</form>

To:

<form action="page.php" method="get">
</form>

PHP
Change references from $_POST[''] to $_GET['']


  • DEMO: http://aseptik.net/php5/demo/deal-with-undesirable-behaviour-when-clicking-the-back-button-on-my-website
<?php
        session_start();        
        if( $_GET['t'] == $_SESSION['token'] ||  $_SESSION['directory'] == $_GET['q'] || !isset($_GET['q']) ) { 
          echo "<h1><span>you going</span> forward</h1>";
        } else {
          echo "<h1><span>you going</span> backward</h1>";
          echo "<p>should be <strong>{$_SESSION['directory']}</strong> directory</p>";
        }        
           $token = uniqid();  
           $_SESSION['token'] = $token;
           $_SESSION['directory'] = $_GET['q'];  
?>

<a href="index.php?q=grammar&t=<? echo $token; ?>">grammar</a>
<a href="index.php?q=vocabulary&t=<? echo $token; ?>">vocabulary</a>

  • Back button re-submit form data ($_POST)
  • Prevent Back button from showing POST confirmation alert


As a general rule in web development you should only do a post when you are submitting data that you don't want to be resubmitted, or that you don't want to show up in your logs on the server. And once you do a post, you should redirect using get parameters or some other method.

If you use get parameters instead of post then you can just make the page navigation stateless and just fetch the page they ask for.

There are methods for detecting if the back button has been pressed, but they are difficult to make work consistently, and can be frustrating to the user, so it is recommended you go about it another way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜