Sessions and Cookies Flashcards
In PHP, sessions and cookies are two mechanisms used to ____ data, but they operate in different ways and serve different purposes.
store user-specific
A session is a _______ mechanism that allows you to store user data across ____ pages during a user’s visit to a website
server-side storage
multiple
What happens when a session is initiated?
A unique session ID is generated & stored on server, sent to browser as a cookie.
Session data is stored on the server and can be accessed using the ____ superglobal array
$_SESSION
To start a session, you use the __ function at the beginning of your script.
session_start()
Write a code for creating a session?
session_start(); // Start the session
$_SESSION[‘username’] = ‘JohnDoe’; // Store session variable
What is the lifetime of a session?
Sessions are temporary and ends when browser is closed.
Data stored in sessions is kept on the ___, which makes it more secure than cookies.
server
What are cookies?
Cookies are small pieces of data stored on the client-side (the user’s browser), sent to server with each request.
What is the lifetime of a cookie?
Persists until expiration date. If no expiration time is set, the cookie will be deleted when the browser is closed.
Where are cookies stored?
On user’s device, making them less secure and unsuitable for sensitive info.
What are cookies often used for?
Tracking user behavior, storing preferences, and maintaining sessions.