What is the difference between storing a User-ID in a cookie versus a session in PHP?

Storing a User-ID in a cookie means that the information is stored on the user's browser, allowing for persistent identification across multiple sessions. On the other hand, storing a User-ID in a session means that the information is stored on the server, providing a more secure and controlled way to manage user data. Using sessions is generally considered more secure as the data is not exposed to the client-side.

// Storing User-ID in a cookie
setcookie("user_id", $user_id, time() + 86400, "/"); // Cookie expires in 1 day

// Storing User-ID in a session
session_start();
$_SESSION["user_id"] = $user_id;