A cookie is a small piece of data stored on the user's computer by the web browser. It helps websites remember information about the user, like login status or preferences.
Click to reveal answer
beginner
How does a server send a cookie to a browser in PHP?
The server sends a cookie using the setcookie() function before any output is sent. This function sets the cookie name, value, and optional parameters like expiration time.
Click to reveal answer
beginner
Where are cookies stored and how are they sent back to the server?
Cookies are stored in the user's browser. When the browser makes a request to the same server, it sends the stored cookies automatically in the HTTP headers.
Click to reveal answer
beginner
What is the purpose of the cookie expiration time?
The expiration time tells the browser how long to keep the cookie. If no expiration is set, the cookie lasts only for the browser session and is deleted when the browser closes.
Click to reveal answer
beginner
How can you access a cookie value in PHP?
You can access cookie values using the $_COOKIE superglobal array with the cookie's name as the key, for example, $_COOKIE['username'].
Click to reveal answer
Which PHP function is used to send a cookie to the browser?
Asetcookie()
Bcookie_send()
Csend_cookie()
Dcookie_set()
✗ Incorrect
The correct function to send a cookie in PHP is setcookie().
Where are cookies stored after being sent by the server?
AOn the user's browser
BOn the server
CIn the PHP script
DIn the database
✗ Incorrect
Cookies are stored on the user's browser and sent back to the server with each request.
What happens if you don't set an expiration time for a cookie?
AThe cookie is stored on the server
BThe cookie lasts forever
CThe cookie lasts only for the browser session
DThe cookie is deleted immediately
✗ Incorrect
Without an expiration time, the cookie lasts only for the current browser session.
How do you access a cookie named 'user' in PHP?
Agetcookie('user')
B$_COOKIE['user']
Ccookie['user']
D$_SESSION['user']
✗ Incorrect
Cookie values are accessed using the $_COOKIE array with the cookie name as the key.
When must you call setcookie() in a PHP script?
AOnly inside functions
BAfter HTML content is printed
CAnywhere in the script
DBefore any output is sent to the browser
✗ Incorrect
Cookies must be sent before any output because they are part of HTTP headers.
Explain how cookies are created, stored, and sent back to the server in PHP.
Think about the journey of a cookie from server to browser and back.
You got /4 concepts.
Describe the role of cookie expiration time and what happens if it is not set.
Consider how long the cookie stays on the user's device.
You got /3 concepts.
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.