Home  >  Article  >  Backend Development  >  PHP防止刷新重复提交页面的示例代码_php实例

PHP防止刷新重复提交页面的示例代码_php实例

WBOY
WBOYOriginal
2016-06-07 17:10:56729browse

PHP防止刷新重复提交页面的示例代码

作为phper,我们在开发和学习php过程中,难免要经常的接受处理表单数据,然而处理表单的时候总会有一个问题,困扰大家,刷新页面重复提交的问题。如何防止刷新页面重复提交呢?

PHP防止刷新重复提交,通过不断刷新(Refresh or Reload)表单提交页面,可以重复提交表单内容,可以利用 PHP 的 Session 来避免这一点,Session 保存在服务器端,在 PHP 过程中改变 Session 变量的值后,即保存在服务器端,下次访问这个变量时,得到是新赋的值,所以,可以用一个 Session 变量记录表单提交的次数,大于1时,就不再处理表单中的数据。

核心代码

以下为引用内容:

if (isset($_POST['action']) && $_POST['action'] == 'submitted') { 
session_start(); 
if (isset($_SESSION['submit_time']) && $_SESSION['submit_time']==0){ 
print '
'; 
print_r($_POST); 
print 'Please try again'; 
print '
'; $_SESSION['submit_time']=1; echo $_SESSION['submit_time']; unset($_SESSION['submit_time']); } else { print '
'; 
print_r($_POST); 
echo "However you have submitted"; 
print '
'; } } else { session_start() or dir("session is not started"); $_SESSION['submit_time']= 0; // isset($_SESSION['submit_time']) or die ("session var is not created"); // echo $_SESSION['submit_time']; ?>
Name:
Email:
Beer:

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