Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a session in PHP.
PHP
<?php
[1]();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like start_session() or init_session() which do not exist.
✗ Incorrect
In PHP, session_start() is used to begin a session and manage state.
2fill in blank
mediumComplete the code to store a user's name in the session.
PHP
<?php session_start(); $_SESSION['username'] = [1]; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string value, causing syntax errors.
✗ Incorrect
To store a string value in the session, it must be quoted. So "John" is correct.
3fill in blank
hardFix the error in the code to correctly check if a session variable exists.
PHP
<?php session_start(); if (isset([1]['username'])) { echo "User is logged in."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or missing the underscore in the session superglobal variable.
✗ Incorrect
The correct superglobal array for session variables is $_SESSION with uppercase letters.
4fill in blank
hardFill both blanks to correctly remove a session variable and destroy the session.
PHP
<?php session_start(); unset([1]['username']); session_[2](); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or wrong function names like session_close().
✗ Incorrect
Use unset($_SESSION['username']) to remove a session variable and session_destroy() to end the session.
5fill in blank
hardFill all three blanks to create a session-based counter that increases on each page load.
PHP
<?php session_start(); if (!isset([1]['count'])) { [1]['count'] = 1; } else { [1]['count'] [2] [3] 1; } echo "Page views: " . [1]['count']; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or operators like =+ which is incorrect.
✗ Incorrect
Use $_SESSION to store the count. Increment it with += and add 1.