19

My problem is that the back button causes the browser to say something like "Page expired" when the previous page was created by a form.

Example:

  • page1: form submitted with search criterias ($_POST request, form points to page2)
  • page2: Receives $_POST request and show result (list of user with links, points to page3)
  • page3: Show user profile

Now when the visitor clicks the back button in the browser it will show something like "Page expired".

Instead the previous page should be shown with no warnings (page2, with the userlist)

How are your strategies to get around this behavior?

Cudos
  • 5,733
  • 11
  • 50
  • 77

6 Answers6

23

If you are submitting a form with search parameters, you are trying to get some data, not modify some.

So, you should use the HTTP GET method, and not POST : POST should be used when you intend to create/modify data, and GET should be used when you intend to fetch some data.

Or, if you have some create/modify operation that has to be done :

  • The form first POSTs to a first page
    • That page does some operations (like writing something to a database)
    • And then redirects to another page, using a Location HTTP header.
  • It's that last page, that's queries by the browser using a GET requests, that displays the data fetched from the parameters received in the URL.

See the Post/Redirect/Get page on wikipedia, about this.

svarog
  • 9,477
  • 4
  • 61
  • 77
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Excellent answer. Couldn't be more clear and to the point. Also giving more than requested; alternatives and more insight. Thanks. – Cudos Jan 27 '10 at 11:46
  • But when I send GET request URL not user friendly, and contains all search data. How can I to resolve this problem? – Dmytro Zarezenko Dec 17 '12 at 09:14
  • An additional question, if you do not mind. I use POST method since my page has to search with some UTF-8 characters(In my case, 'Korean'). It can not deliver right search text with GET. However, since I used POST method, bowser back button does not work. What can I do in this case? Thanks :D b – Juneyoung Oh Jan 30 '15 at 08:34
  • there will be problems in get variables for making clean url for seo.. if some variables are left blanks... –  Sep 02 '17 at 00:26
14

Use the Post/Redirect/Get (PRG) Pattern.

PRG Pattern

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
2

This applies to PHP and IE8.

Not only must you set cacheing to private, but you must remove the 4 cacheing headers and this can only be done with PHP 5.3. In PHP 5.2 you can only set the 4 headers to blank values if using the Zend Framework's setHeader() method. For some reason is not sufficient on IE8 to set the 4 header values to empty values. Here's the code for PHP 5.3:

    header_remove("Expires");
    header_remove("Cache-Control");
    header_remove("Pragma");
    header_remove("Last-Modified");
Chung Lau
  • 31
  • 1
1

Send a Location header in the script you POSTed to, pointing to the page that comes after.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Use HTTP status code 303 for this. PHP defaults to 302 if not specified explicitly according to the current documentation. – MattBianco Jun 05 '14 at 13:49
1

Don't use POST for search. Search can safely be done with GET since it won't alter anything.

Pim Jager
  • 31,965
  • 17
  • 72
  • 98
0

You can use session to do this.

eg.

$_SESSION['name'] = $_POST['name'];

Remember to unset your variables after the process is complete to optimize memory usage.

svarog
  • 9,477
  • 4
  • 61
  • 77