PHP uses session and cookies to prevent repeated submission of instances

Source: Internet
Author: User
In web development, preventing repeated submission is a practical and common problem. besides, you can directly query the database to see if users submit the same data for filtering, we can also prevent this kind of thing from being discovered when the user submits data. next I will introduce sessi-based... in web development, preventing repeated submission is a practical and common problem. besides, you can directly query the database to see if users submit the same data for filtering, we can also prevent such cases from being discovered when users submit data. next we will introduce some implementation methods based on session-based and cookies-based anti-repeated submission.

Prevents refresh or resubmit

Therefore, we should consider adding a parameter to prevent such cases. cookies and sessions are available. However, cookies are client-side. if cookies are disabled, we can refresh the clicks maliciously. Use the MD5 value of the IP + URL parameter as the SESSION name.

Implementation principle setting max_reloadtime = 100; // sets the maximum interval between page refreshes.

When the user opens the page for the first time, the current time of the record is saved in session_start.

The second time the user opens the page (determines whether session_start exists), the difference time_passed is obtained by subtracting the current time from session_start.

When time_passed <max_reloadtime indicates that the user refresh the warning frequently within the specified time and then exits directly

 $ AllowTime) {$ refresh = true; $ _ SESSION [$ allowT] = time ();} else {$ refresh = false;}?>

Prevent repeated submission of forms

 '; Print_r ($ _ POST); print 'Please try again'; print'
'; $ _ SESSION ['num'] = 500;} else {print'
';        print_r($_POST);        echo "However you have submitted";        print '
';}} Else {session_start () or die ("session is not started"); $ _ SESSION ['num'] = 400;?>

For example, a smarty-based demo version

 Assign ('code', $ code);?>

10. //// prevent repeated submission of forms

In the tpl Template

1.

 "> // The code on the receiving page is as follows: session_start (); if (isset ($ _ POST ['originator']). {if ($ _ POST ['originator'] = $ _ SESSION ['code']) {// statement for processing the form, omitted} else {echo '. please do not refresh this page or submit the form again! ';}}?>


Permanent link:

Reprint at will! Include the article address.

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.