PHP implements the function of preventing repeated submission of forms (based on token verification)

不言
Release: 2023-03-28 21:40:02
Original
1663 people have browsed it

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[&#39;token&#39;] = md5(microtime(true));
}
function valid_token() {
  $return = $_REQUEST[&#39;token&#39;] === $_SESSION[&#39;token&#39;] ? true : false;
  set_token();
  return $return;
}
//如果token为空则生成一个token
if(!isset($_SESSION[&#39;token&#39;]) || $_SESSION[&#39;token&#39;]==&#39;&#39;) {
  set_token();
}
if(isset($_POST[&#39;web&#39;])){
  if(!valid_token()){
    echo "token error,请不要重复提交!";
  }else{
    echo &#39;成功提交,Value:&#39;.$_POST[&#39;web&#39;];
  }
}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[&#39;token&#39;]?>">
      <input type="text" class="input" name="web" value="脚本之家">
      <input type="submit" class="btn" value="提交" />
    </form>
  </p>
</p>
</body>
</html>
<?php }?>
Copy after login

##The above is the entire content of this article, thank you for reading. Please pay attention to the PHP Chinese website for more information!

Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template