0
0
PHPprogramming~5 mins

Setting and reading cookies in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a cookie in web development?
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 do you set a cookie in PHP?
You use the setcookie() function. For example: setcookie('user', 'Alice', time() + 3600); sets a cookie named 'user' with value 'Alice' that expires in 1 hour.
Click to reveal answer
beginner
How do you read a cookie value in PHP?
You read cookies from the $_COOKIE superglobal array. For example: $user = $_COOKIE['user'] ?? 'Guest'; gets the 'user' cookie or 'Guest' if it doesn't exist.
Click to reveal answer
intermediate
What must you remember about when to call setcookie() in PHP?
You must call setcookie() before any output is sent to the browser, because it sends HTTP headers. If you print or echo anything first, setting cookies will fail.
Click to reveal answer
intermediate
How can you delete a cookie in PHP?
To delete a cookie, set it with an expiration time in the past. For example: setcookie('user', '', time() - 3600); tells the browser to remove the 'user' cookie.
Click to reveal answer
Which PHP function is used to set a cookie?
Acookie_set()
Bsetcookie()
Cset_cookie()
Dcookie()
Where are cookies stored?
AOn the user's computer
BOn the web server
CIn the PHP script
DIn the database
How do you access a cookie named 'session' in PHP?
A$_SESSION['session']
Bgetcookie('session')
C$_COOKIE['session']
Dcookie('session')
What happens if you call setcookie() after outputting HTML?
AThe cookie will be deleted
BCookie will be set normally
CPHP will automatically buffer output and set cookie
DCookie will not be set because headers are already sent
How do you delete a cookie named 'user'?
Asetcookie('user', '', time() - 3600);
Bunset($_COOKIE['user']);
Csetcookie('user', null);
Ddelete_cookie('user');
Explain how to set and read a cookie in PHP with an example.
Think about how you tell the browser to save data and then how you get it back.
You got /3 concepts.
    What are the important rules to remember when working with cookies in PHP?
    Consider timing and how cookies expire or get removed.
    You got /3 concepts.