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, the problem of repeated submissions after refreshing the page. How to prevent repeated submissions when refreshing the page?
In fact, we will have many methods in PHP learning. For example, the simplest one is that we can use the method of jumping after the submission is successful. But sometimes, there will be many bottlenecks in this way. For example, if our page has a lot of content that needs to be loaded, especially a lot of dealing with the background, then if the page is jumped and reloaded, it may put pressure on the server. Here we can use another method to prevent repeated submissions by refreshing the page, so that everyone can learn PHP better.
We can use session to solve this problem. We first create a new session and assign it a value. After the first submission, we change the value of the session. When we submit this content for the second time, if it is not our assignment, the passed data will not be processed.
Such as:
session_start();
$_SESSION['num'] = 0;
if(isset($_POST['action'] && $_POST['action']=='submit')){
if($_SESSION['num'] == 0){
echo ''Please try again;'
$_SESSION['num'] = 1;
}else{
echo 'You have already submitted, please do not submit again';
}
}
?>
The front page is not given, I believe everyone knows how to write it before. In fact, there are many ways to prevent repeated submissions by refreshing the page. In the future PHP learning exchanges, more will be listed.