简体   繁体   English

何时使用 htmlspecialchars() function?

[英]when to use htmlspecialchars() function?

Hi I was wondering when is the appropriate place to use htmlspecialchars().嗨,我想知道什么时候使用 htmlspecialchars() 合适。 Is it before inserting data to database or when retrieving them from the database?是在将数据插入数据库之前还是从数据库中检索它们时?

You should only call this method when echoing the data into HTML.您应该只在将数据回显到 HTML 时调用此方法。

Don't store escaped HTML in your database;不要将转义的 HTML 存储在您的数据库中; it will just make queries more annoying.它只会使查询更烦人。
The database should store your actual data, not its HTML representation.数据库应该存储您的实际数据,而不是它的 HTML 表示。

You use htmlspecialchars EVERY time you output content within HTML, so it is interperted as content and not HTML.您可以使用htmlspecialchars每次输出内容中的HTML,所以它interperted的内容,而不是HTML。

If you allow content to be treated as HTML, you have just opened the door to bugs at a minimum, and total XSS hacks at worst.如果您允许将内容作为 HTML 处理,那么您至少为漏洞打开了大门,最坏的情况是全面的 XSS 黑客攻击。

Save the exact thing that the user enters into the database.保存用户输入数据库的确切内容。 then when displaying it to public, use htmlspecialchars() , so that it offers some xss protection.然后在向公众显示时,使用htmlspecialchars() ,以便它提供一些 xss 保护。

Guide - How to use htmlspecialchars() function in PHP指南 - 如何在 PHP 中使用 htmlspecialchars() 函数

To begin you have to understand 1 simple concept: Render.首先,您必须了解一个简单的概念:渲染。

What Render is?什么是渲染? Render is when the HTML transforms渲染是 HTML 转换时

<b>Hello</b>

to bold like this Hello .像这样大胆你好 That's render.那是渲染。

So...When to use the htmlspecialchars() function?那么...什么时候使用 htmlspecialchars() 函数?

Wherever you want to render HTML contents.无论您想在何处呈现 HTML 内容。 For example, if you are using JQuery and you do this:例如,如果您使用 JQuery 并执行以下操作:

$("#YourDiv").html("<b>Hello</b>");

The div contents will be Hello . div 内容将是Hello It rendered the text into HTML.它将文本呈现为 HTML。

If you want to display the message in this way (was wrote by user):如果要以这种方式显示消息(由用户编写):

<b>Hello</b>

you have to put:你必须把:

$("#YourDiv").text("<b>Hello</b>");

In that way the Hello will never be rendered.这样,Hello 将永远不会被呈现。

If you want to load the message (as wrote by user) into a textbox, textarea, etc... You have to put:如果您想将消息(由用户编写)加载到文本框、文本区域等中……您必须输入:

<input type="text" class="Texbox1" value="">

<script>
$(".Textbox1").val("<b>Hello</b>");
</script>

That will display那会显示

 <b>Hello</b>

Inside the Textbox without problems.在Textbox里面没有问题。

Conclusion:结论:

What ever data the user input into your forms, etc...Save the data as normally.用户输入到您的表单中的任何数据等等...像往常一样保存数据。 Do not use any function.不要使用任何功能。 If user sent 12345 save as it is.如果用户发送 12345 则按原样保存。 Do not filter nothing.不要过滤任何东西。 You only have to filter when you are going to display the data in the page to the users.您只需要在将页面中的数据显示给用户时进行过滤。 YOU, ONLY YOU decide if you want to render or not what the user wrote.您,只有您自己决定是否要呈现用户所写的内容。 *Remember that. *记住这一点。

Regards!问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM