PHP uses session and cookies to prevent repeated submission of instances

Source: Internet
Author: User
Tags php session

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: Copy code

<? Php
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: Copy code

<? Php
/* 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 '<pre> ';
Print_r ($ _ POST );
Print '<a href = "'. $ _ SERVER ['php _ SELF '].'"> Please try again </a> ';
Print '</pre> ';
$ _ SESSION ['num'] = 500;
} Else {
Print '<pre> ';
Print_r ($ _ POST );
Echo "However you have submitted ";
Print '</pre> ';
}
} Else {
Session_start () or die ("session is not started ");
$ _ SESSION ['num'] = 400;
?>
<Form action = "<? Php echo $ _ SERVER ['php _ SELF '];?> "Method =" POST ">
Name: <input type = "text" name = "personal [name]"> <br>
Email: <input type = "text" name = "personal [email]"> <br>
Beer: <br>
<Select multiple name = "beer []">
<Option value = "warthog"> Warthog </option>
<Option value = "guinness"> Guinness </option>
<Option value = "stuttgarter"> Stuttgarter Schwabenbr </option>
</Select> <br>
<Input type = "hidden" name = "action" value = "submitted">
<Input type = "submit" name = "submit" value = "submit me! ">
</Form>
<? Php
}
?>


For example, a smarty-based demo version

The Code is as follows: Copy code


$ 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: Copy code

1. <input type = "hidden" name = "code" value = "{$ code}"/>

/* 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! ';
}
}

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.