How can cookies be used effectively to maintain user sessions in PHP applications?

To maintain user sessions in PHP applications, cookies can be used effectively to store session identifiers that can be used to identify and authenticate users across multiple requests. By setting a cookie with a unique session ID when a user logs in, the application can use this ID to retrieve session data and maintain the user's session state.

// Start session
session_start();

// Check if user is logged in
if(isset($_SESSION['user_id'])) {
    // Set a cookie with the session ID
    setcookie('session_id', session_id(), time() + 3600, '/');
}