What best practices should be followed when setting up session management in PHP to ensure optimal performance and security?

When setting up session management in PHP, it is important to follow best practices to ensure optimal performance and security. This includes using secure session handling functions, setting appropriate session configuration options, and properly validating and sanitizing session data.

// Start session with secure settings
session_start([
    'cookie_lifetime' => 86400, // 1 day
    'cookie_secure' => true,
    'cookie_httponly' => true,
    'use_strict_mode' => true,
    'use_cookies' => true,
    'use_only_cookies' => true
]);

// Validate and sanitize session data
$_SESSION['user_id'] = filter_var($_SESSION['user_id'], FILTER_SANITIZE_NUMBER_INT);