Click here to Skip to main content
13,833,015 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


76 bookmarked
Posted 9 Dec 2010
Licenced CPOL

Browser back button issue after logout

, 26 Jan 2013
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.
This is an old version of the currently published tip/trick.
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?
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. Apart from that, caching has to be handled.
One needs to clear the cache 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:
// Code disables caching by browser. Hence the back browser button
// grayed out and could not causes the Page_Load event to fire 
You can add something similar in form aspx if you want to place it there:
<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">
one can clear browser history through JavaScript:
//clears browser history and redirects url
<SCRIPT LANGUAGE="javascript"> 
     var Backlen=history.length;   
     window.location.href=page url 
one can set this in logout event:
protected void LogOut()   
     string nextpage = "Logoutt.aspx";       
     Response.Write("<script language=\"javascript\">");             
     Response.Write(" var Backlen=history.length;");       
     Response.Write(" history.go(-Backlen);");       
     Response.Write(" window.location.href='" + nextpage + "'; ");
some other combination of the above options.
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

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
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
professional 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
mentorSandeep 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
professionalRenju 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 
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
mentorSandeep 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
Thomas Daniels5-Jan-13 7:04
mveThomas Daniels5-Jan-13 7:04 
GeneralRe: A syntax error Pin
Sandeep Mewara26-Jan-13 21:08
mentorSandeep 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
Nikhil_S8-Feb-12 19:52
professionalNikhil_S8-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
professionalTejas Vaishnav10-Nov-11 20:15 
GeneralReason for my vote of 5 dfdgdfg Pin
member6024-Oct-11 19:08
membermember6024-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
Sandesh M Patil29-Jun-11 2:33
memberSandesh M Patil29-Jun-11 2:33 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 27 Jan 2013
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid