What are the advantages and disadvantages of using sessions versus cookies in PHP for user authentication?

When implementing user authentication in PHP, using sessions is generally considered more secure than using cookies. Sessions store user data on the server side, making it harder for malicious users to tamper with the data. Cookies, on the other hand, store data on the client side, which can be vulnerable to attacks such as cross-site scripting.

// Start a session
session_start();

// Store user data in the session
$_SESSION['user_id'] = $user_id;

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

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