How can sessions be managed in PHP using cookies?

Sessions can be managed in PHP using cookies by setting the session ID as a cookie. This allows the session to persist even if the user closes the browser. By setting the session ID as a cookie, the session data can be retrieved and maintained across multiple page loads.

// Start the session
session_start();

// Set the session ID as a cookie
setcookie(session_name(), session_id(), time() + 3600, '/');

// Use session variables as needed
$_SESSION['user_id'] = 123;

// Retrieve session variables
$user_id = $_SESSION['user_id'];