0
0
PHPprogramming~10 mins

Cookie expiration and security in PHP - Interactive Code 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('user', 'John', [1]);
?>
Drag options to blanks, or click blank then click option'
Atime() + 3600
B3600
C'3600'
Dtime() - 3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 3600 instead of time() + 3600 causes the cookie to expire immediately.
Passing the expiration time as a string instead of an integer.
2fill in blank
medium

Complete the code to set a secure cookie that is only sent over HTTPS.

PHP
<?php
setcookie('session', 'abc123', time() + 3600, '/', '', [1], true);
?>
Drag options to blanks, or click blank then click option'
A1
Bfalse
Ctrue
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting secure flag to false or 0 allows cookie over HTTP.
Passing secure flag as string instead of boolean.
3fill in blank
hard

Fix the error in the code to set a cookie with HttpOnly flag.

PHP
<?php
setcookie('token', 'xyz', time() + 3600, '/', '', false, [1]);
?>
Drag options to blanks, or click blank then click option'
Afalse
B0
C1
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Setting HttpOnly flag to false leaves cookie vulnerable to XSS.
Passing HttpOnly flag as string instead of boolean.
4fill in blank
hard

Fill both blanks to set a cookie that expires in 2 hours and is restricted to the '/account' path.

PHP
<?php
setcookie('auth', 'token123', [1], [2]);
?>
Drag options to blanks, or click blank then click option'
Atime() + 7200
B'/'
C'/account'
Dtime() + 3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3600 seconds instead of 7200 for 2 hours.
Setting path to '/' instead of '/account'.
5fill in blank
hard

Fill all three blanks to set a cookie with name in uppercase, value from variable, and secure flag enabled.

PHP
<?php
$name = 'sessionid';
$value = 'abc123';
setcookie([1], [2], time() + 3600, '/', '', [3], true);
?>
Drag options to blanks, or click blank then click option'
A$name
B$value
Ctrue
Dstrtoupper($name)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name as string instead of its value.
Not converting the cookie name to uppercase.
Setting secure flag to false.