Bird
Raised Fist0
PHPprogramming~10 mins

Cookie expiration and security in PHP - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does setting the expires parameter in setcookie() control?
easy
A. How long the cookie stays on the user's browser
B. The name of the cookie
C. The value stored inside the cookie
D. The domain where the cookie is valid

Solution

  1. Step 1: Understand the purpose of the expires parameter

    The expires parameter sets the time when the cookie will be deleted by the browser.
  2. Step 2: Relate expires to cookie lifetime

    Setting expires controls how long the cookie remains stored on the user's device.
  3. Final Answer:

    How long the cookie stays on the user's browser -> Option A
  4. Quick Check:

    Cookie expiration = lifetime on browser [OK]
Hint: Expires means cookie lifetime in browser [OK]
Common Mistakes:
  • Confusing expires with cookie name or value
  • Thinking expires controls cookie content
  • Mixing expires with domain or path
2. Which of the following is the correct way to set a cookie with secure and HttpOnly flags in PHP?
easy
A. setcookie('user', 'John', time() + 3600, '/', '', true, false);
B. setcookie('user', 'John', time() + 3600, '/', '', false, true);
C. setcookie('user', 'John', time() + 3600, '/', '', true, true);
D. setcookie('user', 'John', time() + 3600, '/', true, true);

Solution

  1. Step 1: Recall setcookie() parameters order

    The parameters are: name, value, expire, path, domain, secure, httponly.
  2. Step 2: Identify correct secure and httponly flags

    Setting secure to true means cookie sent only over HTTPS, and httponly true means inaccessible to JavaScript.
  3. Final Answer:

    setcookie('user', 'John', time() + 3600, '/', '', true, true); -> Option C
  4. Quick Check:

    Secure and HttpOnly flags = true, true [OK]
Hint: Secure is 6th, HttpOnly is 7th parameter in setcookie [OK]
Common Mistakes:
  • Mixing order of parameters
  • Setting secure or httponly to false by mistake
  • Omitting parameters causing errors
3. What will be the output of this PHP code?
setcookie('test', '123', time() - 3600);
if(isset($_COOKIE['test'])) { echo 'Cookie set'; } else { echo 'Cookie not set'; }
medium
A. Cookie set
B. Cookie not set
C. Syntax error
D. Empty output

Solution

  1. Step 1: Understand the expiration time used

    The cookie is set with an expiration time in the past (time() - 3600), which deletes the cookie immediately.
  2. Step 2: Check if cookie exists after deletion

    Since the cookie is expired, $_COOKIE['test'] will not be set, so the else branch runs.
  3. Final Answer:

    Cookie not set -> Option B
  4. Quick Check:

    Expired cookie = not set [OK]
Hint: Past expiration deletes cookie, so isset returns false [OK]
Common Mistakes:
  • Assuming cookie is set immediately after setcookie
  • Ignoring expiration time meaning
  • Confusing server and client cookie states
4. Identify the error in this PHP code snippet for setting a secure cookie:
setcookie('session', 'abc123', time() + 3600, '/', '', 'true', true);
medium
A. The secure flag is a string instead of boolean
B. The expiration time is in the past
C. The cookie name is invalid
D. Missing path parameter

Solution

  1. Step 1: Check parameter types in setcookie()

    The secure parameter must be a boolean (true/false), not a string.
  2. Step 2: Identify the incorrect usage

    Here, 'true' is a string, which PHP treats as true but is incorrect usage and can cause unexpected behavior.
  3. Final Answer:

    The secure flag is a string instead of boolean -> Option A
  4. Quick Check:

    Secure flag must be boolean, not string [OK]
Hint: Use true/false without quotes for flags [OK]
Common Mistakes:
  • Using 'true' or 'false' as strings
  • Forgetting to set secure flag for HTTPS
  • Omitting required parameters
5. You want to set a cookie named auth that lasts exactly 2 hours, is only sent over HTTPS, and cannot be accessed by JavaScript. Which of the following is the best way to do this?
hard
A. setcookie('auth', 'token', time() + 3600, '/', '', true, false);
B. setcookie('auth', 'token', time() + 7200, '/', '', false, true);
C. setcookie('auth', 'token', time() + 7200, '/', '', true, false);
D. setcookie('auth', 'token', time() + 7200, '/', '', true, true);

Solution

  1. Step 1: Calculate correct expiration time

    2 hours = 7200 seconds, so expiration should be time() + 7200.
  2. Step 2: Set secure and httponly flags correctly

    Secure flag true ensures HTTPS only; httponly true prevents JavaScript access.
  3. Step 3: Verify all parameters

    Path is '/', domain is default '', flags are true, true matching requirements.
  4. Final Answer:

    setcookie('auth', 'token', time() + 7200, '/', '', true, true); -> Option D
  5. Quick Check:

    2 hours + secure + httponly = setcookie('auth', 'token', time() + 7200, '/', '', true, true); [OK]
Hint: Use time()+7200, secure=true, httponly=true for 2hr secure cookie [OK]
Common Mistakes:
  • Using wrong expiration time
  • Not setting secure flag for HTTPS
  • Leaving httponly false allowing JS access
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard