How can PHP sessions be effectively managed to avoid potential pitfalls?

To effectively manage PHP sessions and avoid potential pitfalls, it is important to properly start, use, and destroy sessions. This includes initializing sessions at the beginning of each script, setting session variables securely, and destroying sessions when they are no longer needed.

// Start the session
session_start();

// Set session variables securely
$_SESSION['user_id'] = filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT);

// Destroy the session when it is no longer needed
session_destroy();