asp.net prevents users from repeating a form through the back button-practical tips

Source: Internet
Author: User
Prevent users from submitting a form repeatedly through the back button
<%
Response. Buffer=true
Response. Expires=0
Response. Expiresabsolute=now ()-1
Response. Cachecontrol= "No-cache"
%>
Response. Buffer=true means to indicate whether the output page is buffered, and when the property value is true, the server will not send any information to the client until all programs have finished executing or encountered
<% Response.Flush%> or <% response.end%>
statement, the information for the buffer is freed.
Expires Property
The Expires property specifies how much time is available to cache the paged memory on the browser. If a user returns to this page before a page expires, the version in the buffer is displayed
Grammar
Response.Expires [= number]
Parameters
Number
How many minutes are left before it expires? Setting this parameter to 0 causes the cached page to expire immediately.
Comments
If this property is set more than once on a page, the shortest time is used.
Apply to
Response objects
Response.expires=0 also indicates an immediate expiration, but cannot expire immediately if the client and server are not in a time zone or the client's time earlier than the server. So use a negative number or use Response.expiresabsolute=now ()-to indicate an immediate expiration, and Response.expires=1 to expire in 1 minutes.

ASP Response.expiresabsolute=now ()-1
In an ASP page, this is usually the setting.
Response.expiresabsolute=now ()-1 ' Sets the absolute value of the expiration date to the current time-1, which is out of date
This is because by default the browser loads the page into the cache before it is displayed. Through the above settings, so that the browser does not cache the page, so when the user point "back", because the page is not cached, so the browser can only again by sending HTTP requests to the server to get the page, so that the implementation of restrictions on the user to the same page to do multiple operations.
ASP Response.Expires =-1
In general, when a user requests a Web server, the server replies the request to the client. On the client side, the browser takes caching as a speed-checking strategy, which is to check the cache when the request is made, and if there is a direct cache, do not request the server.
In web programming, sometimes in order to prevent repetitive submissions or strict order, the user's data must be immediately expired after use, and when the user is back, the display expires and cannot continue to use. It's a security concern.
Generally, Response.Expires is written at the top of the page, followed by a time, that is, the expiration time, 0 means to expire immediately.


Settings for browser Caching in ASP
Response.CacheControl = "No-cache"
The settings are not cached in the temporary file. This sentence means that the image you see on the Internet is usually on the Internet


The next thing we're going to talk about is to focus on the back button itself, not the browser cache. Here is an article rewiring the back button is worth reference. I noticed, however, that if you use this method, although the user clicks the Back button, he won't see the previous page, but just click two times, which is not what we want.


effect, because a lot of times, stubborn users can always find a way to bypass the precautionary measures.
Another way to disable the back button is to use client JavaScript to open a window without a toolbar, which makes it difficult for users to return to the previous page, but not impossible. A more secure but annoying way to do this is to open a new window when the form is submitted, while closing the window where the form is located. But I think this method is not worth serious consideration, because I


We can't let the user open a new window every single form is submitted.
So is it possible to add JavaScript code to a page that we don't want the user to return? The JavaScript code added to this page can be used to produce a Click forward button, which also offsets the user's click of the Back button. The JavaScript code to implement this feature is as follows:
<script language= "JavaScript" >
<!--
Javascript:window.history.forward (1);
-->
</script>
Similarly, this method is effective, but far from the "best method". Then I saw someone suggest using Location.replace to move from one page to another. The rationale for this approach is to replace the current history with the URL of the new page, so that there is only one page in the browsing history, and the back button will never become available. I think this may be the way many people are looking for, but it's still not the best way to do it in any situation. Examples of using this method are as follows:
<a href= "pagename.htm" onclick= "Javascript:location.replace" (THIS.HREF); Event.returnvalue=false; ">
No link back to this page
Try this link below:
No link back to this page!
The disadvantage of this approach is that simply using Response.Redirect will no longer work, because every time a user moves from one page to another, we must clear the Location.history with client code. Also note that this method clears the last access history, not all the access records.
Click on the link above and you will open a simple HTML page. Then click the Back button, you can see this is not open this page, but this page before the page! (You must, of course, have client JavaScript code enabled in the browser.) )

After a careful search, I found that I still couldn't find a way to really disable the browser back button completely. All of the methods described here can prevent users from returning to the previous page to varying degrees and in different ways, but they all have their own limitations. Since there is no way to completely disable the back button, the best solution would be to mix client and server-side scripts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.