Prevent php forms from being refreshed and automatically submitted repeatedly-PHP source code

Source: Internet
Author: User
Repeated form submission is definitely not what we need. The most common solution is to directly query the database using php for processing, today, I have sorted out three methods to prevent repeated submission of forms without querying the database. We certainly do not need to submit the forms repeatedly. the most we can do is to directly query the database using php for processing., today, I sorted out three methods to prevent repeated submission by users without querying the database.

Script ec (2); script

Js blocks repeated submission


The first is to tell the browser to disable the submit button after the form is submitted, and the second is to change the button text to give the user some information about what happened. This is the form tag added to your code: onsubmit = "document. getElementById ('myclick '). disabled = true; document. getElementById ('myclick '). value = 'submitting, please wait... '; "Your form tag will be similar:

Or jquery practices

$ (Document). ready (function (){
$ (Input: submit). click (){
SetTimeout (function () {obj. disabled = true;}, 100)
};
});

PHP programmers prevent users from submitting multiple forms. This method is applied to most browsers (IE +, FireFox, Opera ,...).

Session prevents repeated submission

Because the content of the form variable is referenced by $ _ POST ['name'], you may directly destroy $ _ POST ['name'] (unset () after processing the form ()) otherwise, the page may cache the Form Content by default. Therefore, even if $ _ POST ['name'] is destroyed, $ _ POST ['name'] is still assigned a value, which is 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 ){

Echo 'Please try again ';
$ _ SESSION ['num'] = 500;
} Else {

Echo "However you have submitted ";
}
} Else {
Session_start () or die ("session is not started ");
$ _ SESSION ['num'] = 400;
?>

}
?>

Cookie prevents repeated submission

Introduce the cookie mechanism to solve the problem (this method is not recommended because it will be written later)

The code for submitting the page is as follows:


Setcookie ("onlypost", 'T'); // set the cookie, which can contain a time value. For example, some forums can store some of your basic information to prevent bumping.
?>

The code for processing page B. php is as follows:

If ($ _ COOKIE ['onlypost'] = 'T '){
Printr ($ COOKIE );
// Process submitted content if the verification is successful
Print "OK ";
Setcookie ("onlypost", 'F'); // you can delete the cooike value.
}
?>

Jump using the header Function

Once the user clicks the submit button, after the data is processed, the page jumps to another page.

If (isset ($ _ POST ['submit ']) {
Header ('location: success. php'); // after processing data, go to other pages
}

Use databases to add Constraints

Directly add a unique constraint to the database or create a unique index. Once the user commits the statement repeatedly, a warning or prompt is thrown,
This is the most direct and effective method for processing only the data submitted for the first time. It is required that the database design and architecture at the early stage be fully considered.

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.