How can PHP sessions be utilized to control access to message-related functionalities?

To control access to message-related functionalities using PHP sessions, you can set a session variable upon successful login and check this variable before allowing access to message functionalities. If the session variable is not set, the user should be redirected to the login page.

<?php
session_start();

if(!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Code for message-related functionalities here