0
0
PHPprogramming~20 mins

How XSS attacks exploit unescaped output in PHP - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XSS Mastery Badge
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 code snippet?

Consider this PHP code that outputs user input directly without escaping. What will be the output when the input is <script>alert('XSS')</script>?

PHP
<?php
$user_input = "<script>alert('XSS')</script>";
echo "User says: $user_input";
?>
AUser says: <script>alert('XSS')</script>
BUser says: &lt;script&gt;alert('XSS')&lt;/script&gt;
CUser says: alert('XSS')
DUser says: &lt;script&gt;alert(&#039;XSS&#039;)&lt;/script&gt;
Attempts:
2 left
💡 Hint

Think about how PHP outputs strings and what happens if HTML tags are included in the string.

🧠 Conceptual
intermediate
1:30remaining
Which option best describes why unescaped output leads to XSS?

Why does outputting user input directly without escaping or sanitizing cause Cross-Site Scripting (XSS) vulnerabilities?

ABecause escaping input removes all user data before output.
BBecause the browser treats unescaped input as plain text and ignores it.
CBecause unescaped output encrypts the user input before display.
DBecause unescaped input can contain HTML or JavaScript that the browser executes.
Attempts:
2 left
💡 Hint

Think about how browsers handle HTML and JavaScript inside web pages.

🔧 Debug
advanced
2:00remaining
Identify the security flaw in this PHP snippet

Look at this PHP code that outputs a username. What security issue does it have?

PHP
<?php
$username = $_GET['user'];
echo "Welcome, $username!";
?>
AIt properly escapes output to prevent XSS.
BIt uses a deprecated PHP function causing errors.
CIt uses user input directly without escaping, allowing XSS attacks.
DIt sanitizes input but forgets to validate it.
Attempts:
2 left
💡 Hint

Consider what happens if the user input contains HTML or JavaScript.

📝 Syntax
advanced
2:00remaining
Which PHP code correctly escapes output to prevent XSS?

Choose the PHP code snippet that safely outputs user input to prevent XSS attacks.

A<?php echo strip_tags($_GET['input']); ?>
B<?php echo htmlspecialchars($_GET['input'], ENT_QUOTES, 'UTF-8'); ?>
C<?php echo $_GET['input']; ?>
D<?php echo htmlentities($_GET['input']); ?>
Attempts:
2 left
💡 Hint

Think about which function converts special characters to HTML entities including quotes.

🚀 Application
expert
1:30remaining
What will be the number of items in the array after this PHP code runs?

This PHP code collects user inputs into an array but does not escape them. How many items will the array contain?

PHP
<?php
$inputs = [];
$inputs[] = '<script>alert(1)</script>';
$inputs[] = 'hello';
$inputs[] = '<img src=x onerror=alert(2)>';
$inputs[] = 'world';
?>
A4
B3
C0
D2
Attempts:
2 left
💡 Hint

Count how many times the array is appended.