How can you store data from the $_POST array in a session using PHP?

To store data from the $_POST array in a session using PHP, you can simply assign the $_POST values to session variables. This allows you to persist the data across different pages or requests for the same user session. This can be useful when you want to retain form data or user input throughout a user's session on a website.

<?php
session_start();

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Store POST data in session variables
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['email'] = $_POST['email'];
}
?>