Whenever you allow your users to submit text to your website, you need to be careful that you don’t leave any security holes open for malicious users to exploit. If you are ever going to allow user submitted text to be visible by the public you should consider using the htmlentities function to prevent them from running html code and scripts that may be harmful to your visitors.
PHP – Converting HTML into Entities
The htmlentities function takes a string and returns the same string with HTML converted into HTML entities. For example, the string “<script>” would be converted to “<script>”.
By converting the < and > into entities, it prevents the browser from using it as an HTML element and it prevents the code from running if you were to display some user’s input on your website.
This may seem a little complicated, but if you think of the way a browser works, in separate stages, it becomes a little easier. Let’s look at the way the function htmlentities changes the data at three different levels: in PHP, in raw HTML and in the web browser. The sample string is a bad script that will redirect visitors to the malicious user’s own website.
The HTML output of the above script would be as follows:
Safe Raw HTML Code:
If we had not used htmlentities to convert any HTML code into safe entities, this is what the raw HTML code would be and it would have redirect a visitor to example.com.
Dangerous Raw HTML Code:
Those two HTML code examples are what you would see if you were to view source on the web page. However, if you were just viewing the output normally in your browser you would see the following.
You’d see whatever spammer site that the malicious user had sent you to. Probably some herbal supplement site or weight loss pills would be displayed.
When Would You Use htmlentities?
Anytime you allow users to submit content to your website, that other visitors can see, you should consider removing the ability to let them use HTML. Although this will remove a lot of cool things that your users can do, like making heavily customized content, it will prevent your site from a lot of common attacks. With some custom coding you can just remove specific tags from running, but that is beyond the scope of this lesson.
36,195 total views, 13 views today