banner Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please send me a mail. I will try to update them in this web site if possible.
Jul
6th

Various ways to Prevent Users, To GoBack to the Previous Pages

Author: admin | Files under Asp.net, tutorials

Introduction

Recently I worked on a banking project in asp.net. Here, I came across a situation like, “we should not allow the user to go back to the previous page”. At first glance, we feel like doing this is very simple. But, believe me, it is really hell. Problem with this is that there is no way of truly disabling the back button.

As I suffered with this issue, after a good research I thought of investing some time to write this tutorial to help some one who is like me. In this tutorial, I will explain different ways to prevent user going back and I will try list out their drawbacks also. But, remember this, I am saying again, there is no way to truly disable the back button.

For the sake this tutorial, we will create two pages called firstpage.aspx and other as secondpage.aspx. On the Fristpage.aspx add a Hyperlink then, catch the on_click event of the HyperLink and add the following line to it:

  1. protected void Redirectlink1_Click(…….)
  2. {
  3.    Response.Redirect("SecondPage.aspx");
  4. }

Now, when the user clicks on the link, he will be re-directed to the second page.

Using the above two pages I will explain the different approaches by dividing them into following sections:

  • Using the java script history.go and history forward method
  • Remove the page from history using the java script
  • Remove the tool bar of the browser
  • Using sessions
  • Using the HttpCachePolicy Class

Using the Java Script history.go and History Forward Method

We can use java script client side code to prevent users going back. The history.go is first kind of these methods, add this on the page load of the tag as follows:

FirstPage.aspx

  1. <body onLoad="if(history.length>0)history.go(+1)">

Other java script method called as history.forward can be as follows:

  1. <script language="JavaScript">
  2. <!–
  3. javascript:window.history.forward(1);
  4. //–>
  5. </script>

Now, every time when the user goes backs to the FirstPage, it will be automatically redirected to second page.

Drawbacks of this approach are, these are implemented on the client and in JavaScript, if the user disables the java script then this approach will not work.

Remove the page from history using the java script

Instead of the above methods, we can directly remove the page from the history. Now, even if the users press the back button as the page is not their history, user cannot go to the previous page. Looks interesting right!! And it is simple also as follows:

  1. javascript:location.replace(this.href)

Drawbacks are same as the first approach that we are using on the client and in JavaScript, if the user disables the java script then this approach will not work.

Remove the tool bar of the Browser

We can remove the tool bar of browser by opening a new window and referring it to self. No tool bar means no buttons, no navigation etc. this approach may help in some situations. This can be done as follows:

First to create the new window and refer to self , we have add the following lines of code, I am placing them in a function as follows:

  1. <script language =javascript>
  2. function DisableToolBar() {
  3. window.open(‘SmallWindow.aspx’,’smallwindow’,‘toolbar=false;target=_parent’,true);
  4. // just close the window and don’t ask to close it
  5. window.opener = self;
  6. window.close();
  7. }</script>

And I calling this function from the Firstpage.aspx of link’s on_click event as follows:

  1. protected void Redirectlink1_Click(…….)
  2. {
  3.    Redirectlink1.Attributes.Add ("onclick", "CreateWindow()");
  4. }

Using Sessions

Till now, all approaches are on the client side, for these kinds of securities we cannot trust them. Suppose, if the user remembers the URL and enters the URL directly then, the above approaches will not work. So the other option is keeping these securities on the server side using the sessions.

We should keep track of the user page as session variables and when the user request for the page, first we have to check in the session variables whether the user already visited that page, if not, then only allow him to view that page as follows:

  1. if(Session["FirstTimeToPage"].ToString().Length > 0)
  2. {
  3.    Session["FirstTimeToPage"] = string.Empty
  4.    Response.Redirect "/Login.aspx"
  5.    Response.End
  6. }

Drawbacks of this method are, if we have lot of pages on the server then the memory used by session increases for every user. Another drawback is, if the user disables or do not support cookies, then this will not work.

Using the HttpCachePolicy Class

In asp.net there is a class called HttpCachePolicy which contains methods for setting cache-specific HTTP headers and for controlling the ASP.NET page output cache. We can use this function in our asp pages to prevent caching, so that their will no pages to go back.(Smart One, Right!!) This can be implemented usually in the page_init as follows:

  1.  
  2. Protected void Page_Init(object Sender, EventArgs e)
  3. {
  4. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  5. Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
  6. }
  7.  

Conclusion

I explained varies ways to prevent the users to go back to the previous page. But, every approach has limitations. Keep one thing in mind that there is no perfect approach to solve this problem.

Similar Posts - 90% of Users have seen these posts also

Post a Comment