What are the best practices for handling session variables in PHP scripts to ensure compatibility across different server configurations?

When handling session variables in PHP scripts, it's important to ensure compatibility across different server configurations by setting the session cookie parameters explicitly. This includes setting the session cookie path, domain, and secure flag to prevent session hijacking and ensure the session data is properly stored and accessed.

// Set session cookie parameters for compatibility across different server configurations
session_set_cookie_params(0, '/', '', false, true);

// Start the session
session_start();

// Access or set session variables as needed
$_SESSION['user_id'] = 123;