Will
Will

Reputation: 75635

Stop user navigating forward again

If a user has visited a page, and returns to the previous page using 'back', how can I ensure they do not go 'forward' again?

Upvotes: 0

Views: 70

Answers (3)

Alex D
Alex D

Reputation: 30445

Even if you could stop people from using the 'forward' button, people could manually enter the same URL again to go 'forward'.

When you design a web app, you have to assume that users may request any URL and in any order. Design your app so it will work correctly anyways, and so that destructive actions cannot be invoked simply by pressing 'back' or 'forward', or visiting a bookmark.

When the user performs actions which change the state of the application, it is better if those actions are invoked by AJAX calls or posting forms rather than by clicking on links. Remember that GET requests are supposed to be idempotent -- conceptually, a GET is a request to view some information, not to perform an action. Remember also that the concept of a web "page" was originally just that -- like a "page" in a book, which you can look at any number of times. When you turn pages in a book, and then turn back again, you don't expect that some irreversible change will occur simply because you turned the pages.

Upvotes: 1

Eirik Hoem
Eirik Hoem

Reputation: 1310

Look into history.replaceState maybe? See https://developer.mozilla.org/en/DOM/window.history

Upvotes: 0

user321531
user321531

Reputation:

This can be solved with a simple session variable.

page1.php

<?php

if ($_SESSION['page2visited'] != 'YES') {

    echo "<a href='page2.php'>Page 2</a>";

}

?>

page2.php

<?php

if ($_SESSION['page2visited'] != 'YES') {

    echo "Page 2 Contents!";
    $_SESSION['page2visited'] = 'YES';

}
else {

    echo "You have already viewed this page. You cannot see the contents again.";

}

?>

Even if they go forward, this can prevent them from seeing/activating the page2.php content again that they saw/activated previously. (Unless they restart their browser, of course)

Upvotes: 2

Related Questions