What is the purpose of session IDs in PHP and how are they used in web development?

Session IDs in PHP are used to track individual user sessions on a website. They are essential for maintaining user state across multiple page views and interactions. Session IDs are typically stored in cookies or passed through URLs to identify a specific user's session data.

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

// Generate a unique session ID for the user
$session_id = session_id();

// Store the session ID in a cookie for future use
setcookie('session_id', $session_id, time() + 3600, '/');

// Use the session ID to retrieve or store session data
$_SESSION['user_id'] = 123;