What is the purpose of using session IDs in a PHP login script?

Session IDs are used in a PHP login script to keep track of a user's session information, such as their login status and user data. This helps to authenticate users and restrict access to certain pages or features based on their login status. By using session IDs, the server can identify and differentiate between different users, allowing for a more secure and personalized user experience.

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

// Set a session ID for the user
$_SESSION['user_id'] = $user_id;

// Check if the user is logged in
if(isset($_SESSION['user_id'])) {
    // User is logged in, allow access to restricted content
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}