📖 Scenario: You are building a simple website that remembers a visitor's name using cookies. When the visitor enters their name, the website saves it in a cookie so that next time they visit, the site greets them by name automatically.
🎯 Goal: Create a PHP script that sets a cookie with the visitor's name, reads the cookie on subsequent visits, and displays a greeting message using the stored name.
📋 What You'll Learn
Create a variable to hold the visitor's name
Set a cookie with the visitor's name that expires in 1 hour
Check if the cookie exists and read the visitor's name from it
Display a greeting message using the visitor's name from the cookie
💡 Why This Matters
🌍 Real World
Cookies are used by websites to remember users and their preferences, making visits more personal and convenient.
💼 Career
Understanding how cookies work is important for web developers to manage user sessions, preferences, and authentication.
Progress0 / 4 steps
1
Create a variable with the visitor's name
Create a variable called $visitorName and set it to the string 'Alice'.
PHP
Hint
Use $visitorName = 'Alice'; to create the variable.
2
Set a cookie with the visitor's name
Use the setcookie function to set a cookie named 'user' with the value of $visitorName that expires in 3600 seconds (1 hour).
PHP
Hint
Use setcookie('user', $visitorName, time() + 3600); to set the cookie.
3
Check if the cookie exists and read its value
Use an if statement to check if the cookie named 'user' exists in $_COOKIE. If it exists, create a variable called $nameFromCookie and set it to the cookie's value.
PHP
Hint
Use if (isset($_COOKIE['user'])) { $nameFromCookie = $_COOKIE['user']; } to read the cookie.
4
Display a greeting message using the cookie value
Use echo to print the greeting message Hello, followed by the value of $nameFromCookie. If $nameFromCookie is not set, print Hello, guest instead.
PHP
Hint
Use echo "Hello, $nameFromCookie"; if the cookie exists, otherwise echo "Hello, guest";.
Practice
(1/5)
1. What is the primary purpose of cookies in PHP?
easy
A. To execute server-side scripts
B. To style web pages dynamically
C. To create database connections
D. To store small pieces of data on the user's computer
Solution
Step 1: Understand what cookies do
Cookies save small data on the user's computer to remember information like preferences or login status.
Step 2: Identify the correct purpose
Among the options, only storing small data on the user's computer matches the purpose of cookies.
Final Answer:
To store small pieces of data on the user's computer -> Option D
Quick Check:
Cookies store data = B [OK]
Hint: Cookies save data on user device, not server tasks [OK]
Common Mistakes:
Confusing cookies with server-side code execution
Thinking cookies style web pages
Assuming cookies manage database connections
2. Which of the following is the correct syntax to set a cookie named user with value John in PHP?
easy
A. setcookie('user', 'John');
B. cookie_set('user', 'John');
C. set_cookie('user', 'John');
D. setcookie(user, John);
Solution
Step 1: Recall the PHP function to set cookies
The correct PHP function is setcookie() with parameters as strings.
Step 2: Check syntax correctness
setcookie('user', 'John'); uses correct function name and quotes around parameters, making it valid syntax.
Final Answer:
setcookie('user', 'John'); -> Option A
Quick Check:
Correct function and quotes = A [OK]
Hint: Use setcookie() with quoted strings for name and value [OK]
Common Mistakes:
Using wrong function names like cookie_set or set_cookie
Forgetting quotes around cookie name or value
Passing variables without quotes incorrectly
3. What will be the output of the following PHP code?
Each PHP statement must end with a semicolon (;). The setcookie() call is missing this.
Step 2: Verify function name and parameters
The function name and parameters are correct, and expiration time is valid.
Final Answer:
Missing semicolon after setcookie() call -> Option A
Quick Check:
Semicolon missing = C [OK]
Hint: Always end PHP statements with semicolon [OK]
Common Mistakes:
Forgetting semicolon at end of function calls
Thinking expiration time is invalid
Confusing function names
5. You want to create a cookie that lasts for 7 days, is only accessible over HTTPS, and cannot be accessed by JavaScript. Which of the following setcookie() calls correctly achieves this?
hard
A. setcookie('session', 'active', time() + 604800);
B. setcookie('session', 'active', time() + 604800, '/', '', false, true);
C. setcookie('session', 'active', time() + 604800, '/', '', true, true);
D. setcookie('session', 'active', time() + 604800, '/', '', true, false);
Solution
Step 1: Calculate expiration time for 7 days
7 days = 7 * 24 * 3600 = 604800 seconds, so time() + 604800 is correct.
Step 2: Set secure and httponly flags
To make cookie HTTPS-only, set the secure flag to true. To improve security, httponly should also be true.
Step 3: Check options
setcookie('session', 'active', time() + 604800, '/', '', true, true); sets secure and httponly to true, matching requirements. Others either disable secure or httponly or omit parameters.