PHP How to Differentiate Between Form POST and Page Refresh
PHP Code to Distinguish Between a Form Post and a Page Refresh which Prevents Double Form Post Processing on the Server
To distinguish between an actual form submssion of $_POST values and a page reload or refresh prevent multiple form submission processing in php ...
- create a $_SESSION variable such as $_SESSION["form_token"]
- Insert the token into a hidden element on the pages form.
- On the page where the form is posted and processed in php, check for the presence of this token and that this token matches or equals $_SESSION["form_token"].
- If it does then you know you have a form submission and not a page refresh so delete the $_SESSION["form_token"] via unset($_SESSION["form_token"]) so that way if the form is refreshed $_SESSION["form_token"] no longer exists so you know its merely a page reload.
On the Page where the Form is
In the php code ...
In the html code for the form insert this hidden element ...
On the Page where the Form $_POST Values are Processed
The function that checks if this is an actual $_POST or page refresh
The code that calls the function to check if valid $_POST or page refresh