A session in PHP is a way to store data on the server for a user to use across multiple pages. It keeps user information safe and private during their visit.
Click to reveal answer
beginner
What is a cookie in PHP?
A cookie is a small piece of data stored on the user's browser. It helps remember information like login or preferences even after the browser is closed.
Click to reveal answer
intermediate
When should you use a session instead of a cookie?
Use a session when you need to store sensitive data securely on the server or when data should not be visible or modifiable by the user.
Click to reveal answer
beginner
What are the size limits for cookies?
Cookies are limited to about 4KB of data per cookie, so they are not suitable for storing large amounts of information.
Click to reveal answer
intermediate
How does a session maintain user state differently from a cookie?
A session stores data on the server and uses a session ID cookie to link the user. Cookies store all data on the user's browser directly.
Click to reveal answer
Which of the following stores data on the server side?
ALocal Storage
BCookie
CSession
DURL Parameters
✗ Incorrect
Sessions store data on the server, while cookies and local storage store data on the client side.
What is the typical size limit for a cookie?
A4KB
B1MB
C100KB
DNo limit
✗ Incorrect
Cookies are limited to about 4KB of data per cookie.
Which is safer for storing sensitive user data?
ASession
BCookie
CLocal Storage
DAll are equally safe
✗ Incorrect
Sessions keep data on the server, making them safer for sensitive information.
If you want to remember a user's preference after they close the browser, what should you use?
AServer variable
BCookie
CSession
DTemporary file
✗ Incorrect
Cookies can persist data on the user's browser even after closing it.
How does a session identify a user?
ABy using IP address only
BBy storing user data in the URL
CBy saving data in local storage
DUsing a session ID stored in a cookie
✗ Incorrect
Sessions use a session ID cookie to link the user to their server-stored data.
Explain the main differences between sessions and cookies in PHP.
Think about where data is stored and how long it lasts.
You got /4 concepts.
Describe a scenario where using a session is better than a cookie.
Consider when you want to keep data hidden from the user.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the best reason to use a PHP session instead of a cookie?
easy
A. To reduce server memory usage
B. To save user preferences permanently on the browser
C. To store sensitive user data temporarily during a visit
D. To share data between different websites
Solution
Step 1: Understand session purpose
Sessions store data on the server and are good for sensitive or temporary data during a visit.
Step 2: Compare with cookies
Cookies store data on the user's browser and are better for non-sensitive, longer-term data.
Final Answer:
To store sensitive user data temporarily during a visit -> Option C
Quick Check:
Session = temporary sensitive data [OK]
Hint: Use sessions for sensitive, temporary data; cookies for long-term [OK]
Common Mistakes:
Thinking cookies are safer than sessions
Using sessions to store permanent data
Confusing where data is stored
2. Which PHP code snippet correctly starts a session?
easy
A.
B.
C.
D.
Solution
Step 1: Recall PHP session syntax
The correct function to start a session is session_start().
Step 2: Check other options
Other options are not valid PHP functions and will cause errors.
Final Answer:
<?php session_start(); ?> -> Option D
Quick Check:
session_start() starts session [OK]
Hint: Remember exact function name: session_start() [OK]