0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】XSS攻撃への対策

0
Last updated at Posted at 2020-11-24

投稿フォームを作成している際、特に対策をしていない場合XSS攻撃を受けてしまう事を学んだ。

PHPではXSS攻撃の対策としてhtmlspecialchars関数が有効であるのでこの関数を使用してフォームを作成。

この関数を使用することによってフォームに入力された物がただの文字列として認識され、javascripなどを記入されても反応しない様になります。

<html lang="ja">
<body>
<h1>投稿フォーム</h1>
<form action="example.php" method="post">
<input type="text" name="comment"><br/>
<input type="submit" value="送信">
</form>
<?php
$comment = htmlspecialchars($_POST['comment'], ENT_QUOTES, 'UTF-8');
echo $comment;
?>
</body>
</html>

htmlspecialcharsは名前が長いので関数に入れてしまう事も考えたが、このコード自体は短いものであったので今回はそのまま記述。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?