How can PHP sessions be effectively used to pass form data between multiple pages of a multi-page form?

When dealing with a multi-page form in PHP, sessions can be effectively used to store form data temporarily as the user navigates through the different pages. This allows the form data to be easily passed between pages without the need to constantly submit and retrieve it from the server.

<?php
session_start();

// Check if form data has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Store form data in session variables
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['message'] = $_POST['message'];

    // Redirect to the next page of the form
    header("Location: page2.php");
    exit();
}
?>