What is the difference between using cookies and sessions in PHP for storing user data?
Cookies store user data on the client-side, while sessions store user data on the server-side. Cookies are stored in the user's browser and can be accessed by the client, while sessions are stored on the server and are more secure. Cookies have a size limit and can be disabled by the user, while sessions have no size limit and are maintained until the user closes the browser or the session expires.
// Using sessions to store user data
session_start();
// Store user data in session variables
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';
// Access user data from session
echo 'Username: ' . $_SESSION['username'];
echo 'Email: ' . $_SESSION['email'];