Asp.net prevents users from submitting forms repeatedly through the back button

Source: Internet
Author: User

Prevent the user from submitting the form repeatedly using the back button
<%
Response. Buffer = true
Response. Expires = 0
Response. ExpiresAbsolute = now ()-1
Response. CacheControl = "no-cache"
%>
Response. Buffer = true indicates whether the output page is buffered. When the attribute value is True, the server will not send any information to the client until all programs are executed or
<% Response. Flush %> or <% Response. End %>
Statement to release the buffer information.
Expires attributes
The Expires attribute specifies the time before the page cached on the browser Expires. If you return to this page before a page expires, the buffer version is displayed.
Syntax
Response. Expires [= number]
Parameters
Number
The number of minutes before the expiration date. Set this parameter to 0 to immediately expire the cached page.
Note
If this attribute is set multiple times on one page, the shortest time is used.
Apply
Response object
Response. expires = 0 also indicates immediate expiration. However, if the client and server are not in the same time zone or the client time is earlier than the time on the server, it cannot immediately expire. Therefore, the system uses a negative number or Response. ExpiresAbsolute = now ()-1 to indicate immediate expiration. response. expires = 1 indicates expiration in 1 minute.

Asp Response. ExpiresAbsolute = now ()-1
This setting is usually used in asp pages.
Response. ExpiresAbsolute = now ()-1 'sets the absolute value of the expiration time to the current time-1, that is, it expires after it comes out.
This is because by default, the browser first loads the page into the cache and then displays it. through the above settings, the browser does not cache the page. Therefore, after the user clicks "back", the browser can only send an HTTP request to the server to obtain the page because the page is not cached, in this way, users are limited to perform multiple operations on the same webpage.
Response. Expires =-1 in asp
Generally, when a user requests a WEB server, the server returns the request to the client. On the client side, the browser uses cache as a speed-up policy, that is, when a request is sent, the browser checks the cache first. If yes, the browser directly calls the cache instead of requesting the server.
In WEB programming, sometimes in order to prevent repeated submission or strict order, user data must expire immediately after use, when the user moves back, the system displays an expiration date and cannot be used again. This is also a security consideration.
In general, response. expires is written at the top of the page, followed by a time, that is, the expiration time. 0 indicates that it expires immediately.

Browser cache settings in asp
Response. CacheControl = "no-cache"
Set not to cache in temporary files.

Next we will discuss the method to move the button back itself as the center, rather than the browser cache. Here is an article Rewiring the Back Button for reference. However, I have noticed that if this method is used, even though the user does not see the previous data input page when clicking the back button, it only needs to be clicked twice, which is not what we hope.

Results, because many times stubborn users can always find a way to bypass preventive measures.
Another way to disable the back button is to open a window without a toolbar with client javascript, which makes it difficult for users to return to the previous page, but not impossible. A safer but rather annoying method is to open a new window when the form is submitted, while closing the window where the form is located. However, I think this method is not worth considering, Because I

You cannot open a new window every time you submit a form.
So, can we add javascript code to the page we don't want the user to return? The javascript code added to this page can be used to produce the effect of clicking the forward button, which offsets the action generated by clicking the back button. The javascript code used to implement this function is as follows:
<Script language = "javascript">
<! --
Javascript: window. history. forward (1 );
// -->
</Script>
Similarly, although this method is effective, it is still far from the "best method. Later, I saw someone suggested using location. replace to switch from one page to another. The principle of this method is to replace the current history with the URL of the new page, so that there is only one page in the history browsing, and the back button will never become available. I think this may be the method that many people are looking for, but it is still not the best method in any situation. The example using this method is as follows:
<A href = "PageName.htm" onclick = "javascript: location. replace (this. href); event. returnvalue = false;">
Do not go back to the link on this page
Try the following link:
Do not go back to the link on this page!
The disadvantage of this method is that simply using Response. Redirect will no longer work, because every time a user transfers from one page to another, we must use the client code to clear location. history. Note that this method clears the last access history, not all access records.
Click the link above to open a simple HTML page. Click the back button to open the page instead of the page! (Of course, you must enable the client javascript code in the browser .)

After careful searching, I found that I still cannot find a way to completely disable the browser's back button. All the methods described here can prohibit users from returning to the previous page in different ways to varying degrees, but they all have their own limitations. Because there is no way to completely disable the back button, the best solution should be to mix client scripts and server 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.