How can session variables be effectively managed in PHP to ensure user login persistence?

To ensure user login persistence in PHP, session variables can be effectively managed by setting a session cookie with a longer expiration time. This will allow the user to stay logged in for a specified period of time even after closing the browser.

// Start a session
session_start();

// Set the session cookie expiration time to 1 hour (3600 seconds)
ini_set('session.cookie_lifetime', 3600);

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