What is the PRG pattern and how can it be used to prevent form resending in PHP?
The Post/Redirect/Get (PRG) pattern is a web development design pattern used to prevent form resubmission when a user refreshes a page after submitting a form. To implement the PRG pattern in PHP, you can redirect the user to another page after processing the form submission. This way, if the user refreshes the page, the form data will not be resubmitted.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Process form data here
// Redirect to a different page to prevent form resubmission
header("Location: success.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form method="post" action="">
<!-- Form fields go here -->
<input type="submit" value="Submit">
</form>
</body>
</html>