views:

590

answers:

4

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?

A: 

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

Ignacio Vazquez-Abrams
+6  A: 

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

PRG Pattern

Alix Axel
+9  A: 

If you are submitting a for 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.

Pascal MARTIN
Plus, if you use GET, the results page is bookmarkable
adam
Excellent answer. Couldn't be more clear and to the point. Also giving more than requested; alternatives and more insight. Thanks.
Cudos
@Cudos : you're welcome :-) Have fun !
Pascal MARTIN
A: 

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

Pim Jager