0
0
PHPprogramming~10 mins

Starting and using sessions 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'
Astart_session()
Bsession_start()
Cbegin_session()
Dsession_begin()
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'
A'user'
B'username'
C'name'
D'id'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys like 'username' or 'name' instead of 'user'
Forgetting quotes around the key
3fill in blank
hard

Fix the error in the code to correctly start a session and set a variable.

PHP
<?php
[1];
$_SESSION['count'] = 1;
?>
Drag options to blanks, or click blank then click option'
Astart_session()
Bbegin_session()
Csession_begin()
Dsession_start()
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names
Missing parentheses
4fill in blank
hard

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

PHP
<?php
session_start();
if (isset($_SESSION[1]) && $_SESSION[2] === true) {
    echo 'User is logged in';
}
?>
Drag options to blanks, or click blank then click option'
A['logged_in']
B['login']
D['user']
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the two blanks
Using keys like 'login' or 'user' instead of 'logged_in'
5fill in blank
hard

Fill all three blanks to start a session, set 'role' to 'admin', and then print it.

PHP
<?php
[1];
$_SESSION[2] = 'admin';
echo $_SESSION[3];
?>
Drag options to blanks, or click blank then click option'
Asession_start()
B['role']
Dsession_begin()
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function to start session
Using different keys for setting and printing
Forgetting quotes around the key