0
0
PHPprogramming~10 mins

Session variables 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 start a session in PHP.

PHP
<?php
[1];
?>
Drag options to blanks, or click blank then click option'
Asession_start()
Bstart_session()
Csession_begin()
Dbegin_session()
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like start_session() or session_begin().
Forgetting the parentheses after the function name.
2fill in blank
medium

Complete the code to set a session variable named 'user' with value 'Alice'.

PHP
<?php
session_start();
$_SESSION[[1]] = 'Alice';
?>
Drag options to blanks, or click blank then click option'
Auser_name
Buser
C'user'
D"user"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the session key.
Using a different key name than 'user'.
3fill in blank
hard

Fix the error in the code to correctly retrieve the session variable 'user'.

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

Fill both blanks to check if a session variable 'loggedIn' is set and true.

PHP
<?php
session_start();
if (isset($_SESSION[1]loggedIn[2]) && $_SESSION['loggedIn'] === true) {
    echo 'Welcome!';
}
?>
Drag options to blanks, or click blank then click option'
A['
B"
C']
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or missing quotes.
Using double quotes inconsistently.
5fill in blank
hard

Fill all three blanks to unset the session variable 'cart', destroy the session, and redirect to 'index.php'.

PHP
<?php
session_start();
unset($_SESSION[1]cart[2]);
session[3]();
header('Location: index.php');
exit();
?>
Drag options to blanks, or click blank then click option'
A['
B']
Cdestroy
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong brackets or missing quotes in unset.
Using session_close() instead of session_destroy().