0
0
PHPprogramming~20 mins

Session vs cookie decision in PHP - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session vs Cookie Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using sessions?

Consider this PHP code snippet that uses sessions to store a user's name. What will be printed?

PHP
<?php
session_start();
$_SESSION['user'] = 'Alice';
echo $_SESSION['user'];
?>
AEmpty output
Buser
CAlice
DError: Undefined index
Attempts:
2 left
💡 Hint

Sessions store data on the server and can be accessed after starting the session.

🧠 Conceptual
intermediate
2:00remaining
When should you use cookies instead of sessions?

Which situation is best suited for using cookies rather than sessions?

AKeeping user preferences that persist after browser closes
BStoring sensitive user data securely on the server
CStoring large amounts of data for a user
DSharing data between different users
Attempts:
2 left
💡 Hint

Think about where cookies and sessions store data and their lifetime.

Predict Output
advanced
2:00remaining
What error does this PHP code produce?

What error will this PHP code produce when trying to access a cookie that is not set?

PHP
<?php
echo $_COOKIE['theme'];
?>
AFatal error: Undefined variable
BNotice: Undefined index 'theme'
CEmpty string output
DNo output, script stops
Attempts:
2 left
💡 Hint

Accessing an unset array key in PHP triggers a notice.

🧠 Conceptual
advanced
2:00remaining
Why are sessions more secure than cookies for sensitive data?

Choose the best reason why sessions are considered more secure than cookies for storing sensitive information.

ASessions store data on the server, reducing exposure to client attacks
BCookies automatically expire after 5 minutes
CSessions store data on the client side encrypted
DCookies cannot be accessed by JavaScript
Attempts:
2 left
💡 Hint

Think about where the data is stored and who can access it.

🚀 Application
expert
2:00remaining
How many items are stored in the session after this code runs?

Given the following PHP code, how many key-value pairs are stored in the session?

PHP
<?php
session_start();
$_SESSION['user'] = 'Bob';
$_SESSION['cart'] = ['apple', 'banana'];
unset($_SESSION['user']);
$_SESSION['logged_in'] = true;
?>
A1
B0
C3
D2
Attempts:
2 left
💡 Hint

Remember that unset removes a key from the session array.