如何在 PHP 中正确转义 HTML 表单输入默认值? [英] How can I properly escape HTML form input default values in PHP?
本文介绍了如何在 PHP 中正确转义 HTML 表单输入默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定以下两个 HTML/PHP 片段:
Given the following two HTML/PHP snippets:
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" />
和
<textarea name="content"><?php echo $_POST['content']; ?></textarea>
我需要为回显的 $_POST
变量使用什么字符编码?我可以使用任何内置的 PHP 函数吗?
what character encoding do I need to use for the echoed $_POST
variables? Can I use any built-in PHP functions?
请假设 $_POST
值还没有被编码.没有魔术引号 - 什么都没有.
Please assume that the $_POST
values have not been encoded at all yet. No magic quotes - no nothing.
推荐答案
使用 htmlspecialchars($_POST['firstname'])
和 htmlspecialchars($_POST['content'])代码>.
在向用户显示字符串之前,始终使用 htmlspecialchars()
对字符串进行转义.
Always escape strings with htmlspecialchars()
before showing them to the user.
这篇关于如何在 PHP 中正确转义 HTML 表单输入默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文