LoginSignup
0
0

More than 3 years have passed since last update.

【PHP基本】XSS(クロスサイトスクリプティング)対策

Last updated at Posted at 2020-08-19

XSS(クロスサイトスクリプティング)対策

htmlspecialchars()を使えば解決できます。

index.php
<?php
$message = "";
$error = false;
$errorMessage = "未入力です!";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $message = $_POST['message'];

  //バリデーション
  if ($message == '') {
    $error = true;
  }
}

?>

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <title>フォームの練習</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <h1>XSS対策</h1>
  <form action="" method="POST">
    <p>
      <label for="">メッセージ</label><br>
      <?php if ($error) {
        echo "<span id='not_entered'>";
        echo $errorMessage . "<br>";
        echo "</span>";
      } ?>
      <input type="text" name="message">

    </p>
    <p>
      <input type="submit" value="送信">
    </p>
  </form>
  <p><?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?></p>

</body>

</html>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0