What is the difference between using cookies and sessions in PHP for user authentication?

When it comes to user authentication in PHP, the main difference between using cookies and sessions lies in where the data is stored. Cookies store data on the client-side, while sessions store data on the server-side. Sessions are generally considered more secure for storing sensitive user authentication data as they are not accessible to the user. Cookies can be manipulated by the user, potentially leading to security vulnerabilities.

// Using sessions for user authentication
session_start();

// Set user data in session
$_SESSION['user_id'] = 123;

// Check if user is authenticated
if(isset($_SESSION['user_id'])) {
    // User is authenticated
    echo "User is authenticated.";
} else {
    // User is not authenticated
    echo "User is not authenticated.";
}