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();
}
?>