How should session data be stored securely in PHP to prevent unauthorized access?

Session data should be stored securely in PHP by using proper session handling functions, setting secure session configurations, and implementing measures to prevent session hijacking and fixation attacks. One way to achieve this is by storing session data in a secure location on the server and using HTTPS to encrypt data transmission.

// Start a secure session
session_start([
    'cookie_secure' => true,
    'cookie_httponly' => true,
    'use_strict_mode' => true,
]);

// Store session data securely
$_SESSION['user_id'] = $user_id;
$_SESSION['username'] = $username;