How can PHP prevent a script from being executed again when a user clicks "back" or "reload" in the browser?
When a user clicks "back" or "reload" in the browser, a script may be executed again, leading to potential duplicate actions or undesired outcomes. To prevent this, we can use a technique called "POST/Redirect/GET" (PRG) pattern in PHP. This involves redirecting the user to a different URL after processing a form submission, effectively preventing the script from being executed again upon refreshing the page.
// Check if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Process form data
// Redirect to a different URL to prevent script execution on refresh
header("Location: success.php");
exit;
}
Related Questions
- What are the implications of using shorthand syntax, like [] for array initialization, in older versions of PHP like 5.3?
- What parameters should be considered when using mysql_fetch_array() to retrieve data from a database in PHP?
- How can PHP developers prevent exposing source code in case the PHP interpreter fails and outputs unparsed source code?