How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?

✔ Recommended Answer

The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.

The simple solution is to redirect to a new (if not the same) page. This pattern is very common in web applications, and is called Post/Redirect/Get. It's typical for all forms to do a POST, then if successful, you should do a redirect.

Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.

Source: stackoverflow.com

Answered By: burntblark

Method #2

Unfortunately, it is not possible to delete the $_POST variable upon pressing the "Refresh" button on the browser, as the $_POST variable is only set when a form is submitted via the HTTP POST method.

When you refresh the page, the browser simply reloads the current URL and resubmits the last request, which may or may not have been a form submission. If it was a form submission, the $_POST variable will be set again with the submitted values.

However, you can unset or clear the $_POST variable manually using the unset() function in PHP. For example, you could add the following code to your PHP script to unset the $_POST variable:

bash
unset($_POST);

This will remove all values in the $_POST array. However, you should use this with caution, as it may cause unexpected behavior if you still need the $_POST values in your script.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755