Use htmlentities and htmlspecialchars to filter input operations in php, and use htmlspecialchars to filter input operations

Source: Internet
Author: User

Use htmlentities and htmlspecialchars to filter input operations in php, and use htmlspecialchars to filter input operations

This example describes how to filter input in php using htmlentities and htmlspecialchars. We will share this with you for your reference. The details are as follows:

Filtering input (that is, any data from the listed data sources) refers to escaping or deleting insecure characters. Before the data reaches the storage layer of the application, you must filter the input data. This is the first line of defense. If the website's comment form receives html, by default, visitors can add malicious <script> tags to the comment without blocking, as shown below:

<P> my tests </p> <script> alert (123) </script>

The above example. If this comment is not filtered out, malicious code is stored in the database and then rendered in the website tag.

HTML

We can use the htmlentities or htmlspecialchars function to filter html and convert special characters into corresponding html entities.

The htmlentities function converts all special characters corresponding to the "html Entity", such as the currency symbol, euro, pound, and copyright symbol. htmlspecialchars only escapes some special characters, & "'<>

These two functions are silly. By default, they do not escape single quotes.

$ Str = '<a href = "test.html" rel = "external nofollow"> \ 'test Page \' </a> <script> alert (213) </script> '; // No escape single quotes echo $ str; echo "

You need to set 2nd parameters ENT_QUOTES. For details, refer to the php manual.

Echo htmlentities ($ str, ENT_QUOTES, 'utf-8'); // escape echo "

The above examples do not distinguish htmlentities from htmlspecialchars. Some special characters, such as the euro, are replaced below. Htmlentities will escape this, But htmlspecialchars will not

Echo htmlentities ('? <> "'). PHP_EOL; echo" 

Conclusion:Generally, you can use strip_tags to remove html tags when submitting a form. If the rich text editor needs to retain html tags, you can use htmlspecialchars to filter submitted data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.