What are the advantages and disadvantages of using sessions over cookies in PHP for user authentication?
When it comes to user authentication in PHP, using sessions is generally considered more secure than using cookies. Sessions store user data on the server side, making it less vulnerable to tampering by users. However, sessions can consume more server resources compared to cookies, which store data on the client side.
// Start a session
session_start();
// Set session variables
$_SESSION['user_id'] = 123;
// Retrieve session variables
$user_id = $_SESSION['user_id'];
// Destroy the session
session_destroy();