0
0
PHPprogramming~20 mins

Destroying sessions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP session destruction code?

Consider the following PHP code that starts a session, sets a variable, destroys the session, and then tries to access the variable. What will be the output?

PHP
<?php
session_start();
$_SESSION['user'] = 'Alice';
session_destroy();
echo isset($_SESSION['user']) ? $_SESSION['user'] : 'No user';
?>
AAlice
BNo user
CUndefined index: user
DFatal error: session_destroy() expects exactly 0 parameters
Attempts:
2 left
💡 Hint

Think about what happens to session variables after session_destroy() is called.

Predict Output
intermediate
2:00remaining
What happens after unsetting session variables but not destroying the session?

What will this PHP code output?

PHP
<?php
session_start();
$_SESSION['count'] = 5;
unset($_SESSION['count']);
echo isset($_SESSION['count']) ? $_SESSION['count'] : 'Count not set';
?>
AFatal error: Cannot unset session variable
BCount not set
CNotice: Undefined index: count
D5
Attempts:
2 left
💡 Hint

What does unset() do to the session variable?

🔧 Debug
advanced
2:00remaining
Why does this session not destroy properly?

Look at this PHP code snippet intended to destroy a session. Why does it fail to fully destroy the session?

PHP
<?php
session_start();
$_SESSION['logged_in'] = true;
session_destroy();
echo isset($_SESSION['logged_in']) ? 'Still logged in' : 'Logged out';
?>
ABecause <code>$_SESSION</code> variables are automatically deleted after <code>session_destroy()</code>
BBecause <code>session_start()</code> was not called before <code>session_destroy()</code>
CBecause <code>session_destroy()</code> requires a parameter to clear session variables
DBecause <code>session_destroy()</code> does not clear <code>$_SESSION</code> array in current script
Attempts:
2 left
💡 Hint

Check what session_destroy() does and what it does not do.

📝 Syntax
advanced
2:00remaining
Which option correctly destroys a PHP session and clears session variables?

Which of the following PHP code snippets correctly destroys the session and clears all session variables?

A<?php session_start(); $_SESSION = array(); session_destroy(); ?>
B<?php session_start(); session_destroy(); $_SESSION = array(); ?>
C<?php session_destroy(); session_start(); $_SESSION = array(); ?>
D<?php $_SESSION = array(); session_destroy(); session_start(); ?>
Attempts:
2 left
💡 Hint

Remember the order: you must start the session before modifying or destroying it.

🚀 Application
expert
2:00remaining
How many session variables remain after this code runs?

Given this PHP code, how many session variables remain in $_SESSION after execution?

PHP
<?php
session_start();
$_SESSION['a'] = 1;
$_SESSION['b'] = 2;
$_SESSION['c'] = 3;
unset($_SESSION['b']);
session_destroy();
?>
A3
B1
C2
D0
Attempts:
2 left
💡 Hint

Consider what unset() and session_destroy() do to $_SESSION in the current script.