0
0
PHPprogramming~10 mins

How sessions 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 start a session in PHP.

PHP
<?php
[1]();
?>
Drag options to blanks, or click blank then click option'
Asession_start
Bstart_session
Cbegin_session
Dinit_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not exist like start_session()
Forgetting to call the function before outputting HTML
2fill in blank
medium

Complete the code to store a value in the session.

PHP
<?php
session_start();
$_SESSION['user'] = [1];
?>
Drag options to blanks, or click blank then click option'
Auser
BJohn
C"John"
D'user'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a bare word without quotes which causes an error
Using single quotes around the key instead of the value
3fill in blank
hard

Fix the error in the code to correctly check if a session variable is set.

PHP
<?php
session_start();
if (isset([1]['user'])) {
    echo "User is logged in.";
}
?>
Drag options to blanks, or click blank then click option'
A$_session
B$SESSION
CSESSION
D$_SESSION
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or missing the dollar sign in $_SESSION
Using $SESSION or $session which are undefined
4fill in blank
hard

Fill both blanks to correctly unset a session variable and destroy the session.

PHP
<?php
session_start();
unset([1]['user']);
[2]();
?>
Drag options to blanks, or click blank then click option'
A$_SESSION
Bsession_destroy
Cdestroy_session
D$SESSION
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names like $SESSION
Calling a non-existent function like destroy_session()
5fill in blank
hard

Fill all three blanks to create a session variable, check it, and print it.

PHP
<?php
[1]();
if (isset([2]['count'])) {
    echo [3]['count'];
} else {
    echo "No count set.";
}
?>
Drag options to blanks, or click blank then click option'
Asession_start
B$_SESSION
Dsession_destroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using session_destroy() instead of session_start()
Using different variable names for the session array