Click here to Skip to main content
16,020,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have found many solution to block back button on browser. For example window.history.forward() works fine in case of back. But my situation is something like this.

I have 2 forms A and B for example. Now if user is on page A, he clicks the link and goes to page B. From here user can click back and come back on page A.I don't have any problem with that. Now I can prevent user to go back to page B with back button by window.history.forward(), it works fine but I don't want user to go to Page B with Forward button. How can I prevent that?

Hope my question is clear.

Thanks in advance.
Posted

C#
please take a logout page and paste the following code in load event.
protected void Page_Load(object sender, EventArgs e)
       {
           try
           {
               FormsAuthentication.SignOut();
               Session.RemoveAll();
               Session.Abandon();


               //Added By Amin Uddin
               //Used To Protect Back button
               Response.Buffer = true;
               Response.ExpiresAbsolute = DateTime.Now.AddDays(-15);
               Response.Expires = -1500;
               Response.CacheControl = "no-cache";
               Response.Cache.SetNoStore();

               System.Web.HttpCookie cook = Request.Cookies.Get(Common.CommonCookie.cookName.userCook.ToString());
               cook.Expires.AddMilliseconds(1);

               string[] cookies = Request.Cookies.AllKeys;
               foreach (string cookie in cookies)
               {
                   Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
               }


           }
           catch  {
               FormsAuthentication.RedirectToLoginPage();

           }
           finally
           {
               FormsAuthentication.RedirectToLoginPage();
           }
       }

and
write the following jscript in loagin page
XML
<script language="javascript" type="text/javascript">

      window.history.forward(1);

  </script>
  <script language="javascript" type="text/javascript">
      function disableBackButton() {
          window.history.forward();
      }
      setTimeout("disableBackButton()", 0);
  </script>
 
Share this answer
 
Hi i saw your question
i dnt know how to do that in ASP. I did this in PHP so what i did was i check the things with session. I developed web site where user use to log in so i use to keep the username in session, when use use to click the back page or forward...then i use check for whether that session still hold the username or not. If so then let him go to forward or back page else dnt let the use go to back or fwd page. In ASP ur question is just for 2 forms so for that u cant check like that..u will have to check for session or u have to put some check for restricting user to come to page B using Fwd button.
Hope u got your answer
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900