Wie unterscheiden sich Sessions und Cookies in Bezug auf Datenspeicherung und Sicherheit?

Sessions and cookies are both used to store data on the client side, but they differ in terms of data storage and security. Sessions store data on the server side and only send a session ID to the client, which is used to retrieve the data from the server. This makes sessions more secure as the data is not exposed to the client. Cookies, on the other hand, store data directly on the client side, making them less secure as the data can be accessed and modified by the client. To improve security, sensitive data should be stored in sessions rather than cookies.

// Start a session
session_start();

// Store sensitive data in session
$_SESSION['username'] = 'example_user';

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