Is it recommended to use cookies instead of session_id for storing user data in PHP?

It is generally not recommended to use cookies to store sensitive user data in PHP as they can be easily manipulated by users. It is more secure to use session_id to store user data as it is stored server-side and cannot be easily tampered with. You can store user data in the session using the $_SESSION superglobal in PHP.

// Start the session
session_start();

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

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

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