0
0
PHPprogramming~10 mins

Session vs cookie decision 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 start a session in PHP.

PHP
<?php
[1]();
?>
Drag options to blanks, or click blank then click option'
Asession_start
Bcookie_start
Cstart_session
Dinit_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using cookie_start() instead of session_start()
Misspelling the function name
2fill in blank
medium

Complete the code to set a cookie named 'user' with value 'Alice' that expires in 1 hour.

PHP
<?php
setcookie('user', 'Alice', time() [1] 3600);
?>
Drag options to blanks, or click blank then click option'
A-
B/
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction which sets expiration in the past
Using multiplication or division which is incorrect here
3fill in blank
hard

Fix the error in the code to check if a cookie named 'user' exists.

PHP
<?php
if (isset($_COOKIE[1])) {
    echo 'User cookie is set';
}
?>
Drag options to blanks, or click blank then click option'
A{'user'}
B('user')
C['user']
D<'user'>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets
Not quoting the cookie name
4fill in blank
hard

Fill both blanks to store and retrieve a session variable named 'username'.

PHP
<?php
session_start();
$_SESSION[1] = 'Bob';
echo $_SESSION[2];
?>
Drag options to blanks, or click blank then click option'
A['username']
B('username')
C{'username'}
D<'username'>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets
Not quoting the key name
5fill in blank
hard

Fill all three blanks to decide when to use a session or a cookie for storing user data.

PHP
<?php
// Use session when data is [1] and cookie when data is [2]
// Example:
if ($data_is_sensitive) {
    session_start();
    $_SESSION[3] = 'secret';
} else {
    setcookie('info', 'public', time() + 3600);
}
?>
Drag options to blanks, or click blank then click option'
Atemporary
Bpersistent
Csecure
Dnon-sensitive
E['user_data']
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing when to use session vs cookie
Incorrect syntax for session variable key