0
0
PHPprogramming~10 mins

How cookies work in PHP - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a cookie named 'user' with value 'John'.

PHP
<?php
setcookie([1], 'John');
?>
Drag options to blanks, or click blank then click option'
A'user'
B'username'
C'session'
D'cookie'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong cookie name like 'username' or 'session'.
Forgetting to put the cookie name in quotes.
2fill in blank
medium

Complete the code to retrieve the value of the cookie named 'user'.

PHP
<?php
if(isset($_COOKIE[[1]])) {
    echo $_COOKIE['user'];
}
?>
Drag options to blanks, or click blank then click option'
A'session'
B'cookie'
C'user'
D'username'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a cookie with a different name.
Not using quotes around the cookie name.
3fill in blank
hard

Fix the error in the code to set a cookie that expires in 1 hour.

PHP
<?php
setcookie('user', 'John', [1]);
?>
Drag options to blanks, or click blank then click option'
Atime() * 3600
B3600
Ctime() - 3600
Dtime() + 3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 3600 instead of a timestamp.
Using a past time like time() - 3600.
4fill in blank
hard

Fill both blanks to set a cookie named 'theme' with value 'dark' that expires in 7 days.

PHP
<?php
setcookie([1], [2], time() + 7 * 24 * 3600);
?>
Drag options to blanks, or click blank then click option'
A'theme'
B'dark'
C'light'
D'color'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the name and value.
Using wrong strings for name or value.
5fill in blank
hard

Fill all three blanks to delete the cookie named 'user'.

PHP
<?php
setcookie([1], [2], [3]);
?>
Drag options to blanks, or click blank then click option'
A'user'
B''
Ctime() - 3600
D'John'
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting expiration to a past time.
Leaving the value as the old cookie value.