How can PHP be used to keep users logged in until they manually log out or leave the site?

To keep users logged in until they manually log out or leave the site, we can use session cookies in PHP. When a user logs in, we can set a session cookie with a long expiration time. This cookie will be sent with each request, allowing the server to identify the user and keep them logged in.

// Start the session
session_start();

// Set a session cookie with a long expiration time
$session_duration = 3600 * 24 * 30; // 30 days
setcookie(session_name(), session_id(), time() + $session_duration, '/');

// Other login logic here