Home >Backend Development >PHP Tutorial >How to add Token to PHP form to prevent repeated submissions

How to add Token to PHP form to prevent repeated submissions

藏色散人
藏色散人forward
2020-04-06 16:07:322606browse

Token Talk

Token is a token. Its biggest feature is randomness and unpredictability. Normal hackers or software cannot guess it.

So, what is the role of Token? What is the principle?

Token is generally used in two places - to prevent repeated form submissions and anti-csrf attacks (cross-site request forgery).

Both are implemented in principle through session token. When the client requests a page, the server will generate a random number Token, place the Token in the session, and then send the Token to the client (usually by constructing a hidden form). The next time the client submits a request, the Token will be submitted to the server along with the form.

Then, if applied to "anti csrf attack", the server will verify the Token value to determine whether it is equal to the Token value in the session. If it is equal, it can be proved that the request is valid and not forged.

However, if it is used to "prevent repeated form submissions", after the first verification of the same on the server side, the Token value in the session will be updated. If the user submits repeatedly, the second verification judgment will be It failed because the Token in the form submitted by the user has not changed, but the Token in the server-side session has changed.

The above session application is relatively safe, but it is also cumbersome. At the same time, when there are multiple pages and multiple requests, the method of generating multiple Tokens at the same time must be used, which takes up more resources and reduces the execution efficiency. Therefore, the method of storing verification information in cookies can also be used instead of session Token. For example, when dealing with "repeated submissions", the submitted information will be written to the cookie after the first submission. When submitted for the second time, since the cookie already has a submission record, the second submission will fail.

However, cookie storage has a fatal weakness. If the cookie is hijacked (XSS attacks can easily obtain user cookies), then there will be another gameover. Hackers will directly implement CSRF attacks.

<?php
/*
* PHP简单利用token防止表单重复提交
* 此处理方法纯粹是为了给初学者参考
*/
session_start();
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;test&#39;])){
  if(!valid_token()){
    echo "token error";
  }else{
    echo &#39;成功提交,Value:&#39;.$_POST[&#39;test&#39;];
    echo "<br/>";
    echo "token:".$_SESSION[&#39;token&#39;];
  }
}
?>
<form method="post" action="">
  <input type="hidden" name="token" value="<?php echo $_SESSION[&#39;token&#39;]?>">
  <input type="text" name="test" value="Default">
  <input type="submit" value="提交" />
</form>

Recommended learning: PHP video tutorial

The above is the detailed content of How to add Token to PHP form to prevent repeated submissions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete