What are the best practices for handling Session IDs in PHP to ensure security?

To ensure security when handling Session IDs in PHP, it is important to generate strong and unique session IDs, store them securely, and regenerate them periodically to prevent session fixation attacks. Additionally, it is recommended to use HTTPS to encrypt the communication between the client and server to prevent session hijacking.

// Start a secure session
session_start([
    'cookie_secure' => true, // Only send cookies over HTTPS
    'cookie_httponly' => true, // Prevent JavaScript access to cookies
]);

// Regenerate session ID periodically
if (mt_rand(1, 100) == 1) {
    session_regenerate_id(true);
}