PHP generates tokens to prevent forms from repeating commits

Source: Internet
Author: User
Tags php database

1, submit button Reset disabled
When the user submits, the button is immediately set to a unavailable state. This uses JS to achieve.
The pre-submission code is as follows:

$("#submit"). attr ('Disabled','true');
$("#submit"). Val ("being submitted, please wait a moment");
After execution, set the button to its original state.
The code is as follows:

$('#submit'). Removeattr ('Disabled');
$("#submit"). Val ("Determine the submission");
This is only for some simple foreground submitted, if we do a form webmaster submitted to our PHP logic layer file to filter this JS.
Expiration Time Method
The user submit button becomes a token (each time the business submission token is a unique value) and sets the expiration time. When the user submits this, the detection token is consistent and expired, if consistent and does not expire, it is considered to have committed two times
Example

<?php
/*
* PHP easy to use token to prevent the form of repeated submissions
* This processing method is purely for beginners ' reference
*/
Session_Start ();
function Set_token () {
$_session['token'] = MD5 (Microtime (true));
}
function Valid_token () {
$return= $_request['token'] = = = $_session['token'] ?true:false;
Set_token ();
return$return;
}
//generates a token if token is empty
If(!isset ($_session['token']) || $_session['token']=="') {
Set_token ();
}
If(Isset ($_post['Test'])){
if(!valid_token ()) {
Echo"Token error";
}Else{
Echo'successful Commit, Value:'. $_post['Test'];
}
}
?>
<form method="Post"action="">
<input type="Hidden"Name="token"Value="<?php echo $_session[' token ']?>">
<input type="text"Name="Test"Value="Default">
<input type="Submit"Value="Submit"/>
</form>
Method Two

<?php
//Open Session
Session_Start ();
//If there is a commit identity
If(Isset ($_get['Action']) && $_get['Action'] ==='Save'){
//if there is a session and it is the same value as passed in, the submission is counted.
if(Isset ($_session['__open_auth']) && Isset ($_post['Auth']) && $_session['__open_auth'] = = $_post['Auth']){
Print_r ($_post);
$_session['__open_auth'] =NULL;//Clear
}Else{
//Walk up
Header"location:post.php");
}
Exit ();
}
//Authorized
$auth = $_session['__open_auth'] = time ();
?>
<!doctype html>
<meta charset="UTF-8">
<title>post</title>
<body>
<form action="Post.php?action=save"Method="Post">
<ul>
<li>
<input type="Hidden"Name="Auth"Value="<?php echo $auth;? >">
<input type="text"Name="UserName">
</li>
<li>
<input type="Password"Name="Userpass">
</li>
<li>
<input type="Submit"Value="Walk up">
</li>
<li>
<?php echo Time ();?>
</li>
</ul>
</form>
</body>
MySQL PHP database duplicate record prevents

<?php
$link =mysql_connect (' localhost ', ' root ', 'Root’);//get a MySQL database connection
$username =$_get["name"];//get the data from the client table only son.
$q ="SELECT * from usertable where user_name= ' $username '";
mysql_query ("SET NAMES gb2312");//avoid Chinese garbled characters
$rs = mysql_query ($q, $link);//Querying the database
$num _rows = mysql_num_rows ($RS);//total number of rows to get query results
If($num _rows==0)
{
$exec ="INSERT into student (user_name) VALUES ($username)";
mysql_query ("SET NAMES gb2312");
mysql_query ($exec, $link);//insert data into the database without this user (registered users)
Echo"User Registration Successful! ";
}
Else
{
Echo"the user name already exists, please re-select the user name! ";
}
?>
The session expiration method has a very important point is a session set and commit success and unsuccessful when a session is verified, this is a bit like login if the login is successful we have to clear the session this principle is similar.

PHP generates tokens to prevent forms from repeating commits

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.