What is the best practice for redirecting to the previous URL in PHP after form submission?
After submitting a form in PHP, it is common to redirect the user back to the previous page to prevent form resubmission when the user refreshes the page. To achieve this, you can use the $_SERVER['HTTP_REFERER'] variable to get the previous URL and redirect the user back to it using the header() function.
<?php
if(isset($_POST['submit'])){
// Process form data here
// Redirect to previous page
header("Location: " . $_SERVER['HTTP_REFERER']);
exit();
}
?>
Keywords
Related Questions
- Are there any specific security considerations to keep in mind when implementing oAuth in PHP for accessing server data?
- What are some strategies for ensuring that a PHP gallery script remains autonomous and updates dynamically when new images are added to the folders?
- What does the warning "number_format() expects parameter 1 to be double, string given" indicate in PHP?