ThinkPHP method to prevent repeated submission of forms

Source: Internet
Author: User

However, there is a situation that cannot be prevented:

After the user submits the form, click the back button of the browser to return to the form page. At this time, the browser will directly retrieve the page from the cache, so the token verification must be passable.

There are many ways to bypass this problem on the Internet. For example, the location. replace () method is used to replace the current history, but this still has flaws. In extreme cases, if a user switches between pages multiple times, the button may return to the previous form page several times.

The solution is to set Cache-Control: no-cache, no-store in the http header. However, I tried to add <meta http-equiv = "Cache-Control" content = "no-cache, no-store "> the output header (" Cache-control: no-cache, no-store ") in the Action is invalid.

Size: 25.42 K
Size: 345x195
Browsing: 2 times
Click Open new window browse full graph "height =" 195 "src =" http://files.jb51.net/upload/201108/20110808213814539.jpg "width =" 345 "border =" 0 ">

After searching for a long time, I found that the problem lies in the template rendering mechanism of ThinkPHP. Open ThinkPHP/Lib/Think/Core/View. class. php to check the 173rd rows.

header("Cache-control: private"); // Supports page bounce

In order to support page bounce, the original TP forces a Cache-control: private header to be sent before each template is output, which is really helpful.

Comment out this line, delete the TP core cache, and try again. The HTTP response header is changed successfully.

Size: 30.6 K
Size: 502x191
Browsing: 3 times
Click Open new window browse full graph "height =" 191 "src =" http://files.jb51.net/upload/201108/20110808213816334.jpg "width =" 500 "border =" 0 ">

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.