Welcome to Dream.In.Code
Become an Expert!

Join 150,068 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!




prevent user from getting back to the previous page

 
Reply to this topicStart new topic

prevent user from getting back to the previous page

blue1
3 Mar, 2008 - 12:25 AM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 5

Hi , I have went through many website as to how to prevent the user from getting back to the previous page . However i managed to use it in a java form and it really works however i want it to be in the asp.net/c# form.
CODE

<script language="JavaScript">

        java script:window.history.forward(1);
        </script>

I also tried many others but it fails.
like using Response.CacheControl
CODE

<%
Response.Buffer = True
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.CacheControl = "no-cache"
%>

session
CODE

Session.Remove("UserName");
        //        Session.Clear();
        //        Session.Abandon();
        //        Session["UserName"] = null;
        //        Response.Redirect("Login.aspx");

is there anyother way to do it.

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Prevent User From Getting Back To The Previous Page
3 Mar, 2008 - 02:05 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
you can invalidate the user session, if you are maintaining one!!!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Prevent User From Getting Back To The Previous Page
3 Mar, 2008 - 06:49 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well unfortunately there is no fool proof way to prevent the user from going back. You can use JavaScript, but all the user has to do is to disable JavaScript and that trick is shot. You can use Sessions, but if the user doesnt accept cookies then that wont work. You can use the HttpCachePolicy Class to prevent pages from caching, something like this (in the Page_Init Event)

csharp

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



Most people use the JavaScript trick you have already posted, but then again if the user disables JavaScript that wont work either. Actually disabling the Back button isn't possible, think of the trouble programmers could get into if they could access a persons browser through a web application like that, a major security hole.

You could also try something like this:

csharp

Response.Buffer = True
Response.ExpiresAbsolute = System.DateTime.Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"

//if the session variable has a length at all then the
//user has been here before so we need to wipe out
//the session and redirect them to the login page
if(Session["FirstTimeToPage"].ToString().Length > 0)
{
Session["FirstTimeToPage"] = string.Empty
Response.Redirect "/Login.aspx"
Response.End
}

//if we make it this far we can go ahead and load the page



This post has been edited by PsychoCoder: 3 Mar, 2008 - 06:53 PM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Prevent User From Getting Back To The Previous Page
3 Mar, 2008 - 10:29 PM
Post #4

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
You can use Sessions, but if the user doesnt accept cookies then that wont work


In case cookies are disabled, you can use URL rewriting (if suppose that is supported by ASP in case cookies are disabled).
If URL rewriting is not supported, you can use a hidden field to maintain the session.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Prevent User From Getting Back To The Previous Page
3 Mar, 2008 - 10:31 PM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
There are many ways to maintain state, but it is truly impossible to prevent what he is looking for, there is no foolproof way to accomplish it.

This post has been edited by PsychoCoder: 3 Mar, 2008 - 10:31 PM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Prevent User From Getting Back To The Previous Page
3 Mar, 2008 - 10:33 PM
Post #6

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
yeah it has to be a trick done by programmar only.
User is offlineProfile CardPM
+Quote Post

mudassarkhan
RE: Prevent User From Getting Back To The Previous Page
11 Aug, 2008 - 12:00 AM
Post #7

New D.I.C Head
*

Joined: 10 Aug, 2008
Posts: 1

I have found a good way for preventing user's from going back.

Working:

when user clicks back button he is redirected to the same page.

here's the code: add the code to the page which you don't want the user to return using back.

if(history.length>0)
history.go(+1);

call it on body load

basically i increment the browser history. it worked for me Try it.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month