How can PHP developers effectively use header redirection to address the issue of data duplication on page refresh?
When a user refreshes a page that was the result of a form submission, the data from the form can be resubmitted, causing duplication. One way to address this issue is by using header redirection after processing the form data. By redirecting the user to a different page after the form is processed, refreshing the page will not resubmit the form data.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data here
// Redirect to a different page to prevent form resubmission
header("Location: success.php");
exit;
}
?>
Related Questions
- What are the best practices for transitioning from using the deprecated mysql_* extension to mysqli_* or PDO in PHP for database connections?
- How can error_reporting be set to E_ALL in PHP to troubleshoot issues with form submissions?
- What are the best practices for handling unique identifiers (IDs) in PHP when updating or inserting records into a database?