What are the best practices for setting and managing sessions in PHP?

Setting and managing sessions in PHP involves properly initializing sessions, setting session variables, and destroying sessions when they are no longer needed. It is important to securely handle session data to prevent unauthorized access and session hijacking.

// Start a session
session_start();

// Set session variables
$_SESSION['username'] = 'john_doe';
$_SESSION['user_id'] = 123;

// Destroy session when no longer needed
session_destroy();