I have made a simple user login using php . When the user logs in and presses the back button , I want to log out the user. I figure out that if I am able to clear cache when the back button is pressed , it will fulfill my purpose . I guess clearstatcache() function in php will work . How can I trigger this when the back button is pressed ?
Asked
Active
Viewed 320 times
0
-
2This seems like a strange feature. What is your reasoning behind logging out the user if they go back? – El_Vanja May 19 '21 at 10:26
-
I guess i am not being clear in the question .My apologies,I am new to stackoverflow. I want to destroy the session when the back button is pressed . – Anshuman Verma May 19 '21 at 10:48
-
Yes, but why? If I was a user and hitting the back button logged me out, I would be annoyed. Also, are you trying to do this when back button is pressed on any of the pages in your application, or only on one specific page? – El_Vanja May 19 '21 at 10:50
-
My application consists of only a single page i.e my homepage which displays the username . If I press the back button i am redirected to the log in page . On the login page if dont enter any credentials and press the forward button, the username of the previously logged in user is displayed . That is why I want to destroy the session when the back button is pressed – Anshuman Verma May 19 '21 at 10:55
-
Don't. Make a logout link which will do what you want. On the login page, add a check whether a user is already logged in (by checking session). If they are logged in, redirect them to next page. That way they will never be able to land on the login page until they consciously choose to log out. – El_Vanja May 19 '21 at 10:58
-
Can you please explain more about "checking the session" . ? – Anshuman Verma May 19 '21 at 11:13
-
You say you want to destroy the session. So clearly you are storing some user data in the session. At the start of the login page, check if those values are set in the session. If yes, redirect. – El_Vanja May 19 '21 at 11:15
-
Oh I see , but I am curious to know that is it possible in php to destroy the session when the back button is pressed ? – Anshuman Verma May 19 '21 at 12:04
-
I have never tried it. Might be possible combining JS and PHP. See [how to detect the back button click](https://stackoverflow.com/questions/2008806/how-to-detect-if-the-user-clicked-the-back-button) and start from there. – El_Vanja May 19 '21 at 12:08
1 Answers
1
Every time you press the back (or forward) button in a browser, the URL changes, which most of the time means a new request is fired off (from the browser, to the server). You can have a logic that is as follows:
Page 1: Clears all cache, presents user with log-in form.
Page 2: Presents user with logged-in data
If on page 2, the user presses the back button, they will be sent back to page 1, which will clear all cache and present the user with a log-in form.
But this also means that the user cannot do anything in page 2 (like go to page 3), because otherwise, any calls to "back" won't actually go back to page 1.
You can do this. But it's a pretty terrible workflow. My advice is, don't do it, and rather have a "log off" button, that takes the user to page 1 (which will clear cache).

dearsina
- 4,774
- 2
- 28
- 34