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
- What best practices should be followed when handling form data in PHP to prevent SQL injections?
- What are common challenges faced when sorting entries in PHP treeview systems?
- How important is it for PHP developers to conduct thorough research, including utilizing search engines like Google, to find existing solutions or resources for complex programming tasks?