How to stop user from going back to a previous page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

How to stop user from going back to a previous page

Post by raghavan20 »

Are there any scripts or tutorials available to stop user from going back to a previous page. This has to be done since I am working transaction related pages and I do not want the user to go back and he/she should mandatorily use links to modify content submitted.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: How to stop user from going back to a previous page

Post by Roja »

raghavan20 wrote:Are there any scripts or tutorials available to stop user from going back to a previous page. This has to be done since I am working transaction related pages and I do not want the user to go back and he/she should mandatorily use links to modify content submitted.
The solution is a pattern called "PRG". http://www.theserverside.com/patterns/t ... d_id=20936

In a nutshell, you post, then redirect, then get the results. Then if they hit back, they end up at the redirect, which puts them exactly where they were - without changes occuring a second time.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Isn't trying to prevent the use of back a little unreliable. What if a user holds their back button and goes back three or fours pages (like I do from time to time)?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Everah wrote:Isn't trying to prevent the use of back a little unreliable. What if a user holds their back button and goes back three or fours pages (like I do from time to time)?
If they hold it, the browser behaviors (so far, in all the majors) won't go further back. As soon as the first hits, it resolves to the redirect, and subsequent "back" just go to the last one.

However, if the user manually chooses to go back multiple pages (like with the history dropdown), then yes, it will go back that far.

To prevent *that* as well, use a one-time token in the form, so that the form won't process twice. :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

in addition to Roja's solution: if the data on the form is sensitive and should, ideally, be only submitted once, prevent it from being cached. Example: header()
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Roja wrote:
Everah wrote:Isn't trying to prevent the use of back a little unreliable. What if a user holds their back button and goes back three or fours pages (like I do from time to time)?
If they hold it, the browser behaviors (so far, in all the majors) won't go further back. As soon as the first hits, it resolves to the redirect, and subsequent "back" just go to the last one.
What I was saying with this is that in IE, if you click and hold the back button it lists all of the last X number of pages you've been to. In Firefox, there is the big back button (one back) and the little back button (lists all backs). In Opera there is the same functionality. Not sure of the others. So, like just a few minutes ago, I was able to go back like 10 pages at a pop in Firefox. How do you prevent that browser functionality?

In response to the original question, one thing to consider is the placement of "Back" and "Forward" links on the pages you are having your users use. That way they can go "back" by always going forward. Maybe even including a message to not use the browsers back button. Of course, removing the cache for the pages could do the trick also, until someone reloads a page that was cleared from the cache already.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Everah wrote:Of course, removing the cache for the pages could do the trick also, until someone reloads a page that was cleared from the cache already.
Yup. And to when they try to reload the page, they have to manually confirm via popup that they really want to reload the page.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Everah wrote:What I was saying with this is..
Thats a much better way to phrase it. I was trying to capture the difference between clicking back, and the more indepth history system.

In a nutshell, to prevent it, you have to code your form specifically to do so.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

thanks for your answers. how to clear the cache or the history stored in the browser???
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

header()

Code: Select all

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

As far as I could remember and from the PHP manual, JCart that code will not cache a page and user has to refresh the page to see it but what I am asking is to stop user from going any previous pages he/she visited.
Post Reply