Home >Backend Development >PHP Tutorial >PHP sample code to prevent refreshing of repeated submission pages, php sample code_PHP tutorial
PHP sample code to prevent refreshing of duplicate submission pages
As phpers, in the process of developing and learning PHP, we inevitably have to accept and process form data frequently. However, when processing forms, there is always a problem that bothers everyone, such as refreshing the page and submitting repeatedly. How to prevent repeated submissions when refreshing the page?
PHP prevents refresh and repeated submission. By continuously refreshing (Refresh or Reload) the form submission page, the form content can be submitted repeatedly. You can use PHP's Session to avoid this. The Session is saved on the server side and the Session variable is changed during the PHP process. After the value is saved on the server side, the next time you access this variable, you will get the newly assigned value. Therefore, you can use a Session variable to record the number of form submissions. When it is greater than 1, the data in the form will no longer be processed.
Core Code
The following is the quotation:
if (isset($_POST['action']) && $_POST['action'] == 'submitted') { session_start(); if (isset($_SESSION['submit_time']) && $_SESSION['submit_time']==0){ print '<pre class="brush:php;toolbar:false">'; print_r($_POST); print '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>'; 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']; ?> <?php } ?>