Posted by CANbike on Sun, 6 Oct 2013

PHP Form: Clear Data on Refresh or Back Button Press

PHP Form-Clear Data on Refresh-01-thumb.jpgPHP Form-Clear Data on Refresh-02-thumb.jpgPHP Form-Clear Data on Refresh-03-thumb.jpgPHP Form-ob start-thumb.jpg

The following is a technique to clear PHP form data and results when refreshed.

The Problem

A PHP form was created that calculates and displays sensitive results. The form data and results should be cleared when the back button is pressed, the refresh button is pressed, or the PHP page is exited.

The Solution

A solution was derived from the Post/Redirect/Get or PRG method often used in web development.

At least three PHP pages are required. They are the form input page, the form calculation page, and the form result page.

Page1Form.php

<?php
session_start();
$_SESSION['page1']=1;
?>

<html>
<body>
    <form action="Page2Calculate.php" method="post">
        <input type="text" autocomplete="off" name="name" value="" class="textform">
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html>

Page2Calculate.php

<?php
session_start();
?>

<html>
<body>
    <?php
        if (isset($_POST['name']) && isset($_SESSION['page1']))
        {
            #Do calculation here. Store in $_SESSION.
            $_SESSION['page2']="2";

            header('Location: Page3Results.php');
	}
        else
        {
            header('Location: Page1Form.php');
        }
    ?>
</body>
</html>

Page3Results.php

<?php 
session_start();
?>

<html>
<body>
    <?php
        if (isset($_SESSION['page2']))
        {
            # echo results
            session_destroy();
        }
        else
        {
            header('Location: Page1Form.php');
        }
    ?>
</body>
</html>

A Summary

After results are displayed, any refreshes or back button presses will be redirected to the form input page.

Page1Form.php Summary

  • A SESSION is initialized, and a page1 variable is set
  • Default values for the form are set to be blank
  • Autocomplete is turned off to prevent previous historical entries from being displayed
  • Data is sent via POST

Page2Calculate.php Summary

  • POST data and SESSION variable page1 are checked
    • If set, calculations are stored in SESSION and redirected to Page3Results.php
      • A redirect destroys the POST data
    • If not set, a redirect is made to Page1Form.php

Page3Results.php Summary

  • SESSION variable page2 is checked
    • If set, results are displayed and SESSION is destroyed
    • If not set, a redirect is made to Page1Form.php
  • There is no POST data
  • There is no SESSION data after results are displayed


[Hostgator] PHP Warning: Cannot modify header information

Files worked without errors/warnings in testing situations. However, the following error was received when uploaded to Hostgator:

PHP Warning: Cannot modify header information – headers already sent by (output started at …) in … on line …

Solution/Fix:

Add ob_start() to the start of the php files Page2Calculate.php and Page3Results.php

<?php
ob_start();
session_start();
?>

Explanation:

Output cannot be made before a header redirect. The function ob_start() turns buffering on to prevent output until the PHP script has ended.


Related Item(s):