search
HomeBackend DevelopmentPHP TutorialPHP生成token防止表单重复提交

1、提交按钮置disabled

当用户提交后,立即把按钮置为不可用状态。这种用js来实现。

提交前代码如下:


$("#submit").attr('disabled','true');

$("#submit").val("正在提交,请稍等");

执行后,把按钮置为原来状态

代码如下:


$('#submit ').removeAttr('disabled');

$("#submit ").val("确定提交");

这样只是针对一些简单的前台提交了,如果我们自己做个表单站长提交给我们php逻辑层文件就过滤这那个js了。

过期时间法

用户提交按钮后生成一个token(每次业务提交token 为唯一值)存入session,并设置过期时间。当用户再此提交时,检测token是否一致且是否过期,若一致且没有过期,则认为提交了二次

例子


/*

* PHP简单利用token防止表单重复提交

* 此处理方法纯粹是为了给初学者参考

*/

session_start();

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['test'])){

if(!valid_token()){

  echo "token error";

  }else{

  echo '成功提交,Value:'.$_POST['test'];

  }

}

?>

 

 

 

方法二


//开启session

session_start();

//如果有提交标识

if(isset($_GET['action']) && $_GET['action'] === 'save'){

  //如果有session且跟传过来的值一样才算提交

  if(isset($_SESSION['__open_auth']) && isset($_POST['auth']) && $_SESSION['__open_auth'] == $_POST['auth']){

  print_r($_POST);

  $_SESSION['__open_auth'] = null;//清空

  } else {

  //走起

  header("location: post.php");

  }

  exit();

}

//授权

$auth = $_SESSION['__open_auth'] = time();

?>

 

 

post

 

 

       

  •    

       

       

  •    

  •    

       

  •    

  •    

       

  •    

  •    

       

  •  

 

mysql php数据库重复记录防止


$link=mysql_connect(‘localhost’,’root’,’root’); //得到MySQL数据库连接 

$username=$_GET["name"]; //得到从客户端表单传过来的数据 

$q="select * from usertable where user_name='$username'"; 

mysql_query("SET NAMES gb2312"); //避免出现中文乱码 

$rs = mysql_query($q, $link); //查询数据库 

$num_rows = mysql_num_rows($rs); //得到查询结果的总行数 

if($num_rows==0) 

$exec="insert into student (user_name) values ($username)"; 

mysql_query("SET NAMES gb2312"); 

mysql_query($exec, $link); //若没有此用户则将数据插入到数据库(注册用户) 

echo "用户注册成功!"; 

else 

echo "该用户名已存在,请重新选择用户名!"; 

?>

session过期法有非常重要的一点是一个session设置与提交成功与不成功时的一个session验证了,这个有点像登录一样的如果登录成功了我们要清除session这个原理也差不多。

<br />


Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor