How to use token in PHP to prevent repeated submission of forms_php tips

WBOY
Release: 2016-05-16 19:54:30
Original
1818 people have browsed it

The example in this article describes how PHP uses tokens 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>

Copy after login

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

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!