Click here to Skip to main content
12,691,231 members (30,194 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


74 bookmarked

Browser back button issue after logout

, 20 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to stop user from accessing the previous pages once he/she logs out and presses browser back button.
Well, I found a lot of people asking for a resolution to handle the browser's back button once user has logged out.
Typically, users report something like:
I am facing issue in my application's logout scenario. After the user login into website, he/she uses it(website) and when done, they do a logout, which leads them back to login page. But the problem is now, from this login page, if I click the browser back button then it again takes the user back to the previous visited page as if logged in. How can I stop user from viewing the previous page once logged out?

The browser Back button is an option to go back to previously visited pages. The back button can be considered as a pointer that is linked to the page previously visited by the user. Browser keeps a stack of the web pages visited as a doubly-linked list.

The back button works by stepping through the history of HTTP requests which is maintained by the browser itself. This history is stored in browsers cache that consists of the entire page content with resources like image and scripts. This enables browser to navigate backwards and forwards through the browser history and have each page displayed instantly from cache without the delay of having it retransmitted over the internet from the server.

Just to handle the scenario of getting page content from server, browsers have a Refresh button that transmits the request to web server and get back the fresh copy of entire page. Internally, this also replaces the copy of the page in the browser's cache.

So, what's the basic reason behind it? It's, browser's Cache!
Now, what can be done to handle the scenario? Surely on logout event one does clear the session. Post which, browsers cache needs to be handled such that browser has no history (this will make back/forward button in browser grayed out disabled.) Here are various ways of how one can do it:

Option #1: Set Response Cache settings in code-behind file for a page
// Code disables caching by browser.
Option #2: Set META tag for HTTP cache settings in your ASPX page header
<META Http-Equiv="Cache-Control" Content="no-cache"/>
<META Http-Equiv="Pragma" Content="no-cache"/>
<META Http-Equiv="Expires" Content="0"/>
Option #3: Clear browser's history through JavaScript using script tag
//clears browser history and redirects url
<SCRIPT LANGUAGE="javascript">
function ClearHistory()
     var backlen = history.length;
     window.location.href = loggedOutPageUrl
Option #4: Clear browser's history through JavaScript injecting through code-behind file via Response
protected void LogOut()
     string loggedOutPageUrl = "Logout.aspx";
     Response.Write("<script language="'javascript'">");
     Response.Write("function ClearHistory()");
     Response.Write(" var backlen=history.length;");
     Response.Write(" history.go(-backlen);");
     Response.Write(" window.location.href='" + loggedOutPageUrl + "'; ");
Option #5: Clear browser's history through JavaScript injecting through code-behind file via Page.ClientScript
As Chris[^] suggested, I would like to add here itself as a part of the Tip/Trick that I would not suggest to use it and mess with browser's history as this is a bad, bad thing.
One should implement it, only if they really need it and are prepared to accept that it is not a good practice.
Lastly, this would not work if one disables JavaScript.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.

You may also be interested in...


Comments and Discussions

QuestionDon't touch browser history! - vote 1 Pin
Sinisa Hajnal5-Mar-15 1:15
professionalSinisa Hajnal5-Mar-15 1:15 
QuestionWhere to add the ClearHistory function as suggested in JS based solution? Pin
VICK1-Nov-13 1:41
member VICK1-Nov-13 1:41 
GeneralMy vote of 5 Pin
saguptamca17-Jun-13 0:03
professionalsaguptamca17-Jun-13 0:03 
GeneralMy vote of 5 Pin
grijesh__mnit25-Mar-13 4:43
membergrijesh__mnit25-Mar-13 4:43 
GeneralMy vote of 3 Pin
grijesh__mnit25-Mar-13 4:14
membergrijesh__mnit25-Mar-13 4:14 
GeneralRe: My vote of 3 Pin
Sandeep Mewara25-Mar-13 4:23
mvpSandeep Mewara25-Mar-13 4:23 
GeneralRe: My vote of 3 Pin
grijesh__mnit25-Mar-13 4:46
membergrijesh__mnit25-Mar-13 4:46 
GeneralMy vote of 5 Pin
Renju Vinod28-Feb-13 1:56
memberRenju Vinod28-Feb-13 1:56 
QuestionFirefox Pin
Claes Andskär22-Feb-13 23:26
memberClaes Andskär22-Feb-13 23:26 
QuestionThanks Pin
db7uk21-Feb-13 10:20
memberdb7uk21-Feb-13 10:20 
This is good for people to see as it is a frequently asked question! good work!
GeneralMy vote of 1 Pin
Sukh Veer Singh29-Jan-13 23:13
memberSukh Veer Singh29-Jan-13 23:13 
GeneralRe: My vote of 1 Pin
Sandeep Mewara30-Jan-13 0:05
mvpSandeep Mewara30-Jan-13 0:05 
GeneralRe: My vote of 1 Pin
Sukh Veer Singh30-Jan-13 2:59
memberSukh Veer Singh30-Jan-13 2:59 
GeneralMy vote of 4 Pin
Arun Selva Kumar B27-Jan-13 21:16
memberArun Selva Kumar B27-Jan-13 21:16 
GeneralA syntax error Pin
ProgramFOX5-Jan-13 7:04
member ProgramFOX5-Jan-13 7:04 
GeneralRe: A syntax error Pin
Sandeep Mewara26-Jan-13 21:08
mvpSandeep Mewara26-Jan-13 21:08 
GeneralRe: Thanks....i have been clear now...... Pin
Pritesh Aryan30-Apr-11 0:25
memberPritesh Aryan30-Apr-11 0:25 
GeneralReason for my vote of 5 good article to understand caching s... Pin
nikhi _singh8-Feb-12 19:52
membernikhi _singh8-Feb-12 19:52 
GeneralReason for my vote of 5 very helping article Pin
ManojDhobale9-Jan-12 18:51
memberManojDhobale9-Jan-12 18:51 
GeneralReason for my vote of 5 Nice tip Pin
nagendrathecoder28-Dec-11 20:49
membernagendrathecoder28-Dec-11 20:49 
GeneralReason for my vote of 5 good one Pin
Prince Antony G13-Dec-11 2:14
memberPrince Antony G13-Dec-11 2:14 
GeneralReason for my vote of 5 Nice.. My 5 Pin
Tejas_Vaishnav10-Nov-11 20:15
memberTejas_Vaishnav10-Nov-11 20:15 
GeneralReason for my vote of 5 dfdgdfg Pin
PGunge24-Oct-11 19:08
memberPGunge24-Oct-11 19:08 
GeneralIf your deleting all of the browser session history, then it... Pin
zenwalker19857-Oct-11 5:39
memberzenwalker19857-Oct-11 5:39 
GeneralReason for my vote of 5 Good tip Pin
SChristmas29-Jun-11 2:33
memberSChristmas29-Jun-11 2:33 
General@Sandeep, Thanks for more question i have ... Pin
Pritesh Aryan29-Apr-11 23:06
memberPritesh Aryan29-Apr-11 23:06 
GeneralRe: It would depend on the way you do it. If you use History bac... Pin
Sandeep Mewara29-Apr-11 23:24
mvpSandeep Mewara29-Apr-11 23:24 
General@Sandeep.. Though i have not much experience in WEB...i woul... Pin
Pritesh Aryan29-Apr-11 19:56
memberPritesh Aryan29-Apr-11 19:56 
GeneralRe: Thats a the design constraint. I clearly mentioned it is not... Pin
Sandeep Mewara29-Apr-11 20:40
mvpSandeep Mewara29-Apr-11 20:40 
GeneralReason for my vote of 5 Good tip for beginners 'How handle ... Pin
E$w@r17-Mar-11 9:35
memberE$w@r17-Mar-11 9:35 
GeneralVery good, 5. --SA Pin
SAKryukov20-Feb-11 21:32
mvpSAKryukov20-Feb-11 21:32 
GeneralReason for my vote of 5 Nice and valuable tip Pin
Sreejith Gopinathan17-Feb-11 0:10
memberSreejith Gopinathan17-Feb-11 0:10 
GeneralReason for my vote of 5 Nice tip, Sandeep! --SA Pin
SAKryukov4-Feb-11 10:47
mvpSAKryukov4-Feb-11 10:47 
GeneralReason for my vote of 5 Very Nice Tip, Thanks for Sharing. I... Pin
Gandalf - The White30-Jan-11 20:11
memberGandalf - The White30-Jan-11 20:11 
GeneralReason for my vote of 5 Veru useful information, Sandeep, th... Pin
SAKryukov27-Jan-11 21:47
mvpSAKryukov27-Jan-11 21:47 
GeneralReason for my vote of 5 Thanks for sharing this tip Pin
linuxjr27-Jan-11 17:53
memberlinuxjr27-Jan-11 17:53 
GeneralReason for my vote of 1 None of the ways are fool proof. Pin
d@nish 27-Jan-11 11:04
member d@nish 27-Jan-11 11:04 
GeneralEncouraging developers to mess with browser history is a bad... Pin
Chris Maunder23-Jan-11 20:56
adminChris Maunder23-Jan-11 20:56 
GeneralRe: Thanks for your suggestion Chris. I have updated the Tip/Tri... Pin
Sandeep Mewara27-Jan-11 7:53
mvpSandeep Mewara27-Jan-11 7:53 
GeneralRe: Thanks for your suggestion Chris. I have updated the Tip/Tri... Pin
Corruptmind20-May-12 23:25
memberCorruptmind20-May-12 23:25 
GeneralBut Dear Sandeep, it doesn't work with Google Chrome or Fire... Pin
Member 453006420-Jan-11 2:07
memberMember 453006420-Jan-11 2:07 
GeneralReason for my 5: Good tip. Very valuable infomation. Thanks ... Pin
Manfred R. Bihy1-Jan-11 7:29
mvpManfred R. Bihy1-Jan-11 7:29 
GeneralReason for my vote of 5 Good one! This is a very valuable Ti... Pin
Manfred R. Bihy1-Jan-11 7:28
mvpManfred R. Bihy1-Jan-11 7:28 
GeneralReason for my vote of 5 Nice one Sandeep!! This is very comm... Pin
Brij20-Dec-10 21:39
mentorBrij20-Dec-10 21:39 
GeneralReason for my vote of 5 Nice One Pin
Vigneshb620-Dec-10 20:54
memberVigneshb620-Dec-10 20:54 
GeneralReason for my vote of 5 Good one. This question was asked by... Pin
SChristmas19-Dec-10 0:40
memberSChristmas19-Dec-10 0:40 
GeneralGood work Pin
Abdul Quader Mamun16-Dec-10 9:49
memberAbdul Quader Mamun16-Dec-10 9:49 
Generalwindows.history.clear is much better and simple Pin
Xmen W.K.12-Dec-10 2:37
memberXmen W.K.12-Dec-10 2:37 
GeneralReason for my vote of 5 Good one...It will be useful for man... Pin
thatraja9-Dec-10 20:29
mvpthatraja9-Dec-10 20:29 
GeneralThanks for the info and tip! Pin
Walt Fair, Jr.9-Dec-10 14:53
subeditorWalt Fair, Jr.9-Dec-10 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 20 Feb 2013
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid