Bird
Raised Fist0
PHPprogramming~20 mins

Starting and using sessions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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 code?

Consider the following PHP code that uses sessions. What will be printed when this script runs?

PHP
<?php
session_start();
if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 1;
} else {
    $_SESSION['count']++;
}
echo $_SESSION['count'];
?>
A1
B0
C2
DUndefined index error
Attempts:
2 left
💡 Hint

Think about what happens the first time the session variable is set.

❓ Predict Output
intermediate
2:00remaining
What will this PHP code output after two page loads?

This PHP script uses sessions to count visits. What will it output on the second page load?

PHP
<?php
session_start();
if (!isset($_SESSION['visits'])) {
    $_SESSION['visits'] = 0;
}
$_SESSION['visits'] += 1;
echo $_SESSION['visits'];
?>
A1
B0
CError: session not started
D2
Attempts:
2 left
💡 Hint

Remember the session variable keeps its value between page loads.

🔧 Debug
advanced
2:00remaining
Why does this PHP session code cause a warning?

Look at this PHP code snippet. It causes a warning about headers already sent. Why?

PHP
<?php
echo "Hello";
session_start();
$_SESSION['user'] = 'Alice';
?>
AThe session variable 'user' is not initialized
Bsession_start() is deprecated after echo
Csession_start() must be called before any output is sent
DYou cannot set session variables after echo
Attempts:
2 left
💡 Hint

Think about when PHP sends HTTP headers.

📝 Syntax
advanced
2:00remaining
Which option correctly starts a session and sets a variable?

Choose the correct PHP code that starts a session and sets the session variable 'role' to 'admin'.

A
&lt;?php
start_session();
$_SESSION['role'] = 'admin';
?&gt;
B
&lt;?php
session_start();
$_SESSION['role'] = 'admin';
?&gt;
C
&lt;?php
session_start;
$_SESSION['role'] = 'admin';
?&gt;
D
&lt;?php
session_start();
session['role'] = 'admin';
?&gt;
Attempts:
2 left
💡 Hint

Check the function name and syntax carefully.

🚀 Application
expert
3:00remaining
What is the value of $_SESSION['total'] after this code runs?

Given this PHP code, what will be the value of $_SESSION['total'] after execution?

PHP
<?php
session_start();
if (!isset($_SESSION['total'])) {
    $_SESSION['total'] = 5;
}
function addToTotal($value) {
    $_SESSION['total'] += $value;
}
addToTotal(10);
addToTotal(-3);
?>
A12
B15
C5
DError: Undefined index 'total'
Attempts:
2 left
💡 Hint

Calculate the additions step by step.

Practice

(1/5)
1. What is the main purpose of using session_start() in PHP?
easy
A. To begin a session and enable storing data across pages
B. To end a session and clear all session data
C. To connect to a database for user authentication
D. To send cookies to the user's browser

Solution

  1. Step 1: Understand what session_start() does

    This function initializes a session or resumes the current one based on a session identifier passed via a cookie or URL.
  2. Step 2: Recognize session purpose

    Sessions allow data to be saved and accessed across multiple pages for the same user, such as login status or preferences.
  3. Final Answer:

    To begin a session and enable storing data across pages -> Option A
  4. Quick Check:

    session_start() starts session = D [OK]
Hint: Remember: session_start() must run before any output [OK]
Common Mistakes:
  • Thinking session_start() ends a session
  • Confusing sessions with database connections
  • Calling session_start() after HTML output
2. Which of the following is the correct way to start a session in PHP?
easy
A. session.begin();
B. start_session();
C. session_start();
D. begin_session();

Solution

  1. Step 1: Recall PHP session function syntax

    The correct function to start a session is session_start() with an underscore and parentheses.
  2. Step 2: Check other options for syntax errors

    Options B, C, and D are not valid PHP functions and will cause errors.
  3. Final Answer:

    session_start(); -> Option C
  4. Quick Check:

    Correct PHP session start syntax = A [OK]
Hint: Use exact function name: session_start() [OK]
Common Mistakes:
  • Using wrong function names like start_session()
  • Missing parentheses after session_start
  • Using camelCase instead of underscore
3. What will be the output of this PHP code?
<?php
session_start();
$_SESSION['user'] = 'Alice';
echo $_SESSION['user'];
?>
medium
A. user
B. Alice
C. Array
D. Undefined index error

Solution

  1. Step 1: Start session and assign value

    The code starts a session and sets the session variable 'user' to 'Alice'.
  2. Step 2: Output the session variable

    Using echo $_SESSION['user']; prints the value stored, which is 'Alice'.
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    $_SESSION['user'] = 'Alice' outputs Alice [OK]
Hint: Session variables store values like arrays [OK]
Common Mistakes:
  • Expecting 'user' instead of its value
  • Forgetting to call session_start() first
  • Confusing session variables with regular variables
4. Identify the error in this PHP code snippet:
<?php
$_SESSION['count'] = 1;
echo $_SESSION['count'];
?>
medium
A. Cannot echo session variables
B. Incorrect variable name $_SESSION
C. Session variables must be integers only
D. Missing session_start() before using $_SESSION

Solution

  1. Step 1: Check session initialization

    The code uses $_SESSION but never calls session_start() to begin the session.
  2. Step 2: Understand session usage rules

    Without calling session_start(), PHP does not know which session to use, causing errors or empty values.
  3. Final Answer:

    Missing session_start() before using $_SESSION -> Option D
  4. Quick Check:

    Always call session_start() before $_SESSION [OK]
Hint: Always start session before accessing $_SESSION [OK]
Common Mistakes:
  • Using $_SESSION without session_start()
  • Assuming $_SESSION works like normal variables
  • Trying to echo $_SESSION without initialization
5. You want to count how many times a user visits a page using sessions. Which code correctly increments the visit count each time the page loads?
<?php
// Fill in the missing part

if (isset($_SESSION['visits'])) {
    $_SESSION['visits']++;
} else {
    $_SESSION['visits'] = 1;
}
echo "Visit number: " . $_SESSION['visits'];
?>
hard
A. session_start();
B. session_destroy();
C. session_unset();
D. session_write_close();

Solution

  1. Step 1: Recognize session must be started

    To use $_SESSION variables, the session must be started with session_start(); at the top.
  2. Step 2: Understand the code logic

    The code checks if 'visits' exists in the session, increments it if yes, or sets it to 1 if no, then prints the count.
  3. Final Answer:

    session_start(); -> Option A
  4. Quick Check:

    Start session before using $_SESSION to track visits [OK]
Hint: Always call session_start() before session variables [OK]
Common Mistakes:
  • Forgetting session_start() causes no session data
  • Using session_destroy() resets visits
  • Confusing session_unset() with starting session
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard