📖 Scenario: You are building a simple PHP web page that shows user comments. This page does not escape user input before showing it. This can cause a security problem called Cross-Site Scripting (XSS).XSS happens when a user enters code that runs in other users' browsers, which can steal information or cause harm.
🎯 Goal: Learn how unescaped output can let harmful code run on a web page by creating a PHP script that shows user comments without escaping, then see how an attacker can add a script tag.
📋 What You'll Learn
Create an array called comments with user comments including a normal comment and a comment with a script tag
Create a variable called output to hold the combined comments as a string
Use a foreach loop with variables comment to add each comment to output without escaping
Print the output variable to show the comments on the page
💡 Why This Matters
🌍 Real World
Web developers must always escape user input before showing it on pages to protect users from XSS attacks.
💼 Career
Understanding XSS and safe output handling is essential for secure web development jobs and protecting websites from hackers.
Progress0 / 4 steps
1
Create the comments array
Create an array called comments with these exact entries: 'Hello, this is a safe comment.' and ''
PHP
Hint
Use $comments = [ ... ]; to create the array with the exact two strings.
2
Create the output variable
Create a variable called output and set it to an empty string ""
PHP
Hint
Use $output = ""; to start with an empty string.
3
Add comments to output without escaping
Use a foreach loop with variable comment to add each comment from $comments to $output by concatenating $comment and a line break "<br>" without escaping
PHP
Hint
Use foreach ($comments as $comment) { $output .= $comment . "<br>"; } to add each comment with a line break.
4
Print the output
Write echo $output; to display the combined comments on the page
PHP
Hint
Use echo $output; to show the comments on the page.
Practice
(1/5)
1. What is the main reason XSS attacks happen in PHP applications?
easy
A. Using too many PHP functions
B. PHP code has syntax errors
C. User input is displayed without escaping special characters
D. Database connection fails
Solution
Step 1: Understand XSS attack cause
XSS attacks exploit when user input is shown directly in HTML without cleaning.
Step 2: Identify the vulnerability
Displaying user input without escaping special characters allows malicious scripts to run.
Final Answer:
User input is displayed without escaping special characters -> Option C
Quick Check:
XSS = unescaped user input [OK]
Hint: XSS = unsafe output from user input [OK]
Common Mistakes:
Thinking syntax errors cause XSS
Confusing database errors with XSS
Believing PHP functions cause XSS
2. Which PHP function is commonly used to prevent XSS by escaping output?
easy
A. json_encode()
B. strtoupper()
C. array_merge()
D. htmlspecialchars()
Solution
Step 1: Identify escaping function
htmlspecialchars() converts special characters to HTML entities, preventing script execution.