PHP uses session and cookies to prevent repeated submission of instances _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP uses session and cookies to prevent repeated instance submission. 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, 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

The code is as follows:

Session_start ();
$ K = $ _ GET ['K'];
$ T = $ _ GET ['t'];
// Anti-refresh time
$ AllowTime = 1800;
$ Ip = get_client_ip ();
$ AllowT = md5 ($ ip. $ k. $ t );
If (! Isset ($ _ SESSION [$ allowT]) {
$ Refresh = true;
$ _ SESSION [$ allowT] = time ();
} Elseif (time ()-$ _ SESSION [$ allowT]> $ allowTime ){
$ Refresh = true;
$ _ SESSION [$ allowT] = time ();
} Else {
$ Refresh = false;
}
?>


Prevent repeated submission of forms

The code is as follows:

/* Ultimate Edition
PHP prevents users from refreshing pages (Refresh or Reload) and submitting the form content repeatedly.
Because the content of the form variable is referenced by $ _ POST ['name'], you may directly destroy $ _ POST ['name'] (unset () after processing the form ()) you can. Actually not. Because the page caches the form content by default, even if $ _ POST ['name'] is destroyed, after refreshing, $ _ POST ['name'] will still be assigned, which is equally valid.
You can use Session to solve the problem. First, assign a value to the Session, such as 400. after the first successful submission, change the Session value. when the second submission, check the value of this Session. if it is not 400, the data in the form is no longer processed.
Can I set the validity period of a Session?
*/
If (isset ($ _ POST ['action']) & $ _ POST ['action'] = 'submitted '){
Session_start ();
Isset ($ _ SESSION ['num']) or die ("no session ");
If ($ _ SESSION ['num'] = 400 ){
Print'

’;       
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

The code is as follows:


$ Code = mt_rand (usd00 );
Setcookie ('addtids', $ code, time () + 300 );
If (isset ($ _ POST ['submit ']) {
If ($ _ COOKIE ['addtids']! = $ _ POST ['code']) {
Echo "Please do not refresh this page or submit the form repeatedly ";
Exit ();
}
}
$ Smarty-> assign ('code', $ code );

10. //// prevent repeated submission of forms

In the tpl Template

The code is as follows:

1.

/* The PHP Session function can also prevent repeated submission of PHP forms. The Session is stored on the server. during PHP running, you can change the Session variable. The next time you access this variable, you get a new value. therefore, you can use a Session variable to record the value submitted in the form. if the value does not match, it is considered that the user has submitted the data repeatedly.
*/


Session_start (); // Generate a random number based on the current SESSION
$ Code = mt_rand (usd00 );
$ _ SESSION ['code'] = $ code;
// Hide and pass in the form:
<Input type = "hidden" name = "originator" value = "<? = $ Code?> ">

// The code on the receiving page is as follows:


Session_start ();
If (isset ($ _ POST ['originator']) {
If ($ _ POST ['originator'] =
$ _ SESSION ['code']) {
// The statement for processing the form, omitted
} Else {
Echo 'Please do not refresh this page or
Submit the form again! ';
}
}

Success ,...

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.