Search code examples
htmlhref

html and href click only once


I have a link

<a href="new link">click me</a>

when a user click the link "click me", the new link page will be redirected and do something at the server side. The problem, if a user click the back button on the browser, it will go back to the old link. If the user click the link "click me", the new link page will be opened and processing at the server side will do again. How to prevent this situation? That is even a user go back to the old page, the new link cannot be clicked anymore


Solution

  • Generate a unique id on the link, like therequest.do?id=1234, and log 1234 to a database so that the request cannot be run again.

    • User goes to mypage.do, server generates id 1234 and logs to database as unprocessed.
    • User clicks link to therequest.do?id=1234
    • Server does processing, and marks id 1234 as processed.
    • User clicks back button.
    • User clicks link again.
    • Server checks and sees request 1234 is already processed, so generates an error message on the screen: request has already been processed.