How does PHP automatically handle Session IDs and data storage for users?

PHP automatically handles session IDs and data storage for users by generating a unique session ID for each user, storing it in a cookie on the user's browser, and saving session data on the server. This allows PHP to associate each user with their session data across multiple page requests.

// Start a new or resume an existing session
session_start();

// Store data in the session
$_SESSION['username'] = 'JohnDoe';

// Retrieve data from the session
$username = $_SESSION['username'];

// Destroy the session when the user logs out
session_destroy();