Home  >  Article  >  Backend Development  >  How to use token in PHP to prevent repeated submission of forms, token form_PHP tutorial

How to use token in PHP to prevent repeated submission of forms, token form_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:54:25859browse

How PHP uses token to prevent repeated submission of forms, token form

This article describes the example of how PHP uses token to prevent repeated submission of forms. Share it with everyone for your reference, the details are as follows:

<&#63;php
/*
* PHP使用token防止表单重复提交
* 此处理方法纯粹是为了给初学者参考
*/
session_start();
function set_token() {
  $_SESSION['token'] = md5(microtime(true));
}
function valid_token() {
  $return = $_REQUEST['token'] === $_SESSION['token'] &#63; 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'];
  }
}
&#63;>
<form method="post" action="">
  <input type="hidden" name="token" value="<&#63;php echo $_SESSION['token']&#63;>">
  <input type="text" name="test" value="Default">
  <input type="submit" value="提交" />
</form>

Readers who are interested in more PHP related content can check out the special topics of this site: "php curl usage summary", "PHP operation and operator usage summary", "PHP network programming skills summary", "PHP basic syntax introductory tutorial" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP Token Token Improved Version
  • PHP Token (Token) Design
  • Solving the Implementation of Repeated Submissions of PHP Forms Method
  • PHP prevents repeated submission of forms by recording IP. >
  • http://www.bkjia.com/PHPjc/1119994.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/1119994.htmlTechArticleHow PHP uses tokens to prevent repeated submissions of forms. The token form example in this article describes how PHP uses tokens to prevent repeated submissions of forms. method. Share it with everyone for your reference, the details are as follows:...
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