Ajax-thinkphp prevents adding a HASH value to <form> during local submission

Source: Internet
Author: User
To prevent form local submission, thinkphp adds a 32-bit hash value to each form tag by default. Any form submission must verify that the hash is consistent with the SESSION on the server. However, what if I use Ajax ?! How to Prevent users from submitting malicious parameters locally. Another... to prevent form local submission, thinkphp adds a 32-bit hash value to each form tag by default. Any form submission must verify that the hash is consistent with the SESSION on the server. However, what if I use Ajax ?! How to Prevent users from submitting malicious parameters locally.
In addition, after verifying that the hash and SESSION are consistent, thinkphp immediately destroys the SESSION and generates a new SESSION and hash value, so this method is not for GET request refresh once again is invalid, such as a website site search function: In the GET method to submit parameters: http://www.example.com/search.php? Query = the best movie to watch & hash = jf75zg93s5su56si8d63hd8km01jf737ey. I refresh it again. The HASH value hasn't changed, but the server SESSION has changed. Will it be blocked? I am very grateful to you for your patience.

Reply content:

To prevent form local submission, thinkphp adds a 32-bit hash value to each form tag by default. Any form submission must verify that the hash is consistent with the SESSION on the server. However, what if I use Ajax ?! How to Prevent users from submitting malicious parameters locally.
In addition, after verifying that the hash and SESSION are consistent, thinkphp immediately destroys the SESSION and generates a new SESSION and hash value, so this method is not for GET request refresh once again is invalid, such as a website site search function: In the GET method to submit parameters: http://www.example.com/search.php? Query = the best movie to watch & hash = jf75zg93s5su56si8d63hd8km01jf737ey. I refresh it again. The HASH value hasn't changed, but the server SESSION has changed. Will it be blocked? I am very grateful to you for your patience.

Hash should be a CSRF Token. You only need to bind the beforeSend event before sending the ajax request and add the token to the header.

For example:

When using ajax requests, you must set 'x-CSRF-token' in the http header. The value is the Token generated by the server, for example

Send ajax in jquery and set 'x-CSRF-token'

$. Ajax ({type: "POST", url: _ node. attr ('href '), beforeSend: function (req) {req. setRequestHeader ('x-CSRF-token', "# {csrf_token}")}, // omitted later });

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.