Friday, August 7, 2009

The problem of fu#king "Back" button..

Sorry for my indecent title but I was so irritated by this "back" button that I cud'nt stop myself.
After a user logs out himself from a web page and presses the "back" button of his browser he again gets himself logged in. The problem really makes you scratch your head sometimes. I did the same and came out with a cool solution.

The page in which you are asking user to log out just write the two statements on page load():

Response.CacheControl = "no-cache";
Response.Cache.AppendCacheExtension("no-store");

The two statements help in controlling the cache and preventing it to save the previous session.

Also make a session variable for login of a user. Make the session variable "true" whenever a user logs in and "false" when he logs out. Check on page load() of logout page, if session variable holds "false" value, direct the page to login page. Now if the user clicks back button he'll still remain in the login page or the page you wish to direct him.
My codes:

protected void Page_Load(object sender, EventArgs e)
{
Response.CacheControl = "no-cache";
Response.Cache.AppendCacheExtension("no-store");
if (Session["log"] != "true")
{
Response.Redirect("expired.aspx");
}
}

* * * * *

No comments:

Post a Comment