This article mainly introduces the method of PHP using token to prevent repeated submission of forms. It generates an encrypted random number and stores it in the token variable of the session, and at the same time puts the value into the form to hide the submission, so as to prevent repeated submission of the form. Function, friends who need it can refer to
<?php /* * 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']; } } ?> <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="提交" /> </form>
Summary: The above is the entire content of this article, I hope it can be helpful to everyone's study.
Related recommendations:
Detailed explanation of function examples for PHP to download files
PHP uses the header method to implement the file download function
PHP method to generate color QR code based on QRCODE
##
The above is the detailed content of How to use token in PHP to prevent repeated submission of forms. For more information, please follow other related articles on the PHP Chinese website!