What are the best practices for handling session variables in PHP when a user closes their browser?

When a user closes their browser, the session variables in PHP are not automatically destroyed. To ensure that session variables are properly handled when a user closes their browser, you can set a session cookie with a specific expiration time. This way, the session will be automatically destroyed when the cookie expires, even if the user closes their browser.

// Set session cookie with specific expiration time
ini_set('session.cookie_lifetime', 0);
session_start();