これは私のフォームの 1 つです (PHP + MySQL、TinyMCE に置き換えられたテキストエリア)。段落、箇条書き、見出し、およびテキストの配置 (右、左、中央、両端揃え) を使用して説明を記録します。
送信されると、レコードは次のように表示されます。
<p style="text-align: justify;"><strong>Introduction</strong></p>
<p style="text-align: justify;">The death of the pixel leaves you with a flowing, magazine-quality canvas to design for. A canvas where curves are curves, not ugly pixel approximations of curves. A canvas that begins to blur the line between what we consider to be real and what we consider to be virtual.</p>
<p style="text-align: justify;">It wasn't too long ago that there was one set of rules for use of type on print and use of type on screen. Now that we have screens that are essentially print quality, we have to reevaluate these conventions.</p>
<p style="text-align: justify;">Web sites are transforming from boring fields of Arial to embrace the gamut of typographical possibilities offered by web fonts. Web fonts, combined with the style and layout options presented by the creative use of CSS and JavaScript offer a new world of typographic oppor</p>
<ol>
<li style="text-align: justify;">point 1</li>
<li style="text-align: justify;">point 2</li>
<li style="text-align: justify;">point 3</li>
</ol>
XSSを回避するためにデータベースに入るデータをサニタイズする必要があることを読み、解決策を探し始めました。
私が見つけた解決策は、「htmlspecialchars()」を使用することです (出典: Lynda.com - セキュアな PHP Web サイトの作成)。
そのため、チュートリアルでは、データベースに保存する前に入力をサニタイズし、(サンプル コード) のようなものを使用する必要があると述べています。
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$category_description = $_POST['category_description'];
echo $category_description;
echo '<br><br>';
echo htmlspecialchars($category_description);
echo '<br><br>';
echo htmlentities($category_description);
echo '<br><br>';
echo strip_tags($category_description);
}
?>
XSSを避けるため。
ここまでわかる。htmlspecialchars() 関数はいくつかの事前定義された文字を HTML エンティティに変換し、htmlentities() は文字を HTML エンティティに変換し、strip_tags() はすべてのタグを完全に削除します。
しかし、htmlspecialchars()、htmlentities()、strip_tags() を使用した後、出力は次のようにレンダリングされます。
これは安全だと思いますが、データベースから取得したときにフロントページで見栄えがよくありません.
htmlspecialchars または htmlentities を介して渡された入力をレンダリングするにはどうすればよいですか?