// Process form on page B if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Do form processing here // Redirect back to page A header('Location: ' . $_SERVER['HTTP_REFERER']); exit; }
// Process login form if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Check if login is successful // Redirect user back to previous page if (isset($_SESSION['prev_page'])) { header('Location: ' . $_SESSION['prev_page']); unset($_SESSION['prev_page']); exit; } else { // Redirect to default page header('Location: home.php'); exit; } } // Set session variable for previous page if (!isset($_SESSION['prev_page'])) { $_SESSION['prev_page'] = $_SERVER['HTTP_REFERER']; }In this example, we first store the URL of the previous page in a session variable `$_SESSION['prev_page']`. After successful login, we check if this session variable is set and redirect the user back to the previous page using `header()` function. If the session variable is not set, we redirect to the default page. The package library used in these examples is the standard PHP library, as the `header()` function and `$_SERVER` and `$_SESSION` variables are part of the core PHP language.