×

php · level 2 · unit 4 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

a visitor's text before you show it on a page.

Web pages are written in . Tags in angle brackets, like <b> for bold, tell the browser what to do. If a visitor types a tag into your form and you echo it onto a page, the browser obeys it.

With a <script> tag, a stranger could run their own code on everyone's screen. That attack is called . The cure is to escape the text: swap each special character for a safe code, like &lt; for <, so the browser shows it instead of obeying it.

escapes for you. Store what the visitor typed as it is, and escape it every time you show it.

<?php
$_POST = ["comment" => "<b>Great</b> cakes & tea"];
$comment = $_POST["comment"];
echo htmlspecialchars($comment) . "\n";
echo htmlspecialchars('Zoe said "wow"') . "\n";

Gwecode shows your output as plain text, so you see the safe codes themselves, the way a browser receives them.