What are the differences between using cookies and sessions in PHP?
Cookies and sessions are both used to store data on the client side in PHP, but they have some key differences. Cookies are stored on the client's browser and have a limited size (usually around 4KB). They are sent with every request to the server, which can impact performance. Sessions, on the other hand, store data on the server and only send a session ID to the client, which is used to retrieve the data on the server side. Sessions are more secure as the data is not exposed to the client.
// Using sessions in PHP
session_start();
// Set session data
$_SESSION['username'] = 'JohnDoe';
// Retrieve session data
echo $_SESSION['username'];