📖 Scenario: You are building a simple PHP web application that needs to remember user preferences. You want to decide whether to use sessions or cookies to store this information.
🎯 Goal: Create a PHP script that stores a user preference using a session or a cookie based on a configuration variable. Then display the stored preference.
📋 What You'll Learn
Create an array called userPreferences with keys 'theme' and 'fontSize' and values 'dark' and 'medium' respectively.
Create a boolean variable called useSession and set it to true or false to decide storage method.
Write code that stores the userPreferences in a session if useSession is true, or in cookies if useSession is false.
Write code that retrieves and prints the stored preferences from the session or cookies.
💡 Why This Matters
🌍 Real World
Web applications often need to remember user settings like themes or font sizes. Sessions and cookies are common ways to store this information.
💼 Career
Understanding sessions and cookies is essential for web developers to manage user data securely and efficiently.
Progress0 / 4 steps
1
Create the user preferences array
Create an array called userPreferences with these exact entries: 'theme' => 'dark' and 'fontSize' => 'medium'.
PHP
Hint
Use PHP array syntax with square brackets and key => value pairs.
2
Add the storage method configuration
Create a boolean variable called useSession and set it to true.
PHP
Hint
Use = true; to assign the boolean value.
3
Store preferences using session or cookie
Write code that starts the session with session_start(). Then, if useSession is true, store userPreferences in $_SESSION['prefs']. Otherwise, store each preference in a cookie using setcookie() with keys 'theme' and 'fontSize' and expiry time 1 hour from now.
PHP
Hint
Remember to call session_start() before using $_SESSION.
4
Retrieve and display stored preferences
Write code that starts the session with session_start(). Then, if useSession is true, retrieve preferences from $_SESSION['prefs']. Otherwise, retrieve preferences from $_COOKIE['theme'] and $_COOKIE['fontSize']. Finally, print the preferences in the format: Theme: [theme], Font Size: [fontSize].
PHP
Hint
Use print() with double quotes and curly braces to insert variables.
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]