This article mainly introduces the function of preventing repeated submission of forms in PHP, and analyzes the relevant operating techniques of PHP based on token verification to prevent repeated submission of forms. It is very simple and practical. Friends in need can refer to it
The example in this article describes how PHP implements the function of preventing repeated submission of forms. I share it with you for your reference, as follows:
There are many ways to prevent repeated submission of forms, so today I will introduce to you how to effectively prevent repeated submission of forms in PHP.
The code is very simple
I believe you are very smart and share a small demo with you, you can learn from it:
Specific code:
<?php /* * 2016年9月29日08:09:13 */ session_start(); header("Content-Type: text/html;charset=utf-8"); function set_token() { $_SESSION['token'] = md5(microtime(true)); } function valid_token() { $return = $_REQUEST['token'] === $_SESSION['token'] ? true : false; set_token(); return $return; } //如果token为空则生成一个token if(!isset($_SESSION['token']) || $_SESSION['token']=='') { set_token(); } if(isset($_POST['web'])){ if(!valid_token()){ echo "token error,请不要重复提交!"; }else{ echo '成功提交,Value:'.$_POST['web']; } }else{ ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>PHP防止重复提交表单</title> <meta name="keywords" content="PHP" /> <meta name="description" content="PHP防止重复提交表单" /> </head> <body> <p id="main"> <p class="demo"> <form method="post" action=""> <input type="hidden" name="token" value="<?php echo $_SESSION['token']?>"> <input type="text" class="input" name="web" value="脚本之家"> <input type="submit" class="btn" value="提交" /> </form> </p> </p> </body> </html> <?php }?>
PHP implements the function of uploading files through CURL
tp framework (thinkPHP) implements account locking after three incorrect login passwords Function
PHP implements the function of generating data dictionary
The above is the detailed content of PHP implements the function of preventing repeated submission of forms (based on token verification). For more information, please follow other related articles on the PHP Chinese website!