Sanitizing input but output not as expected

孤街浪徒 提交于 2019-12-02 02:40:07
maytham-ɯɐɥʇʎɐɯ

My suggestion is to build a function to sanitize all your text inputs and a function to check all your outputs that comes from the database or any other sources, like following:

<?php
// filter for user input
function filterInput($content)
{
    $content = trim($content);
    $content = stripslashes($content);

    return $content;
}

//filter for viewing data
function filterOutput($content)
{
    $content = htmlentities($content, ENT_NOQUOTES);
    $content = nl2br($content, false);

    return $content;
}

depending on your strategy, you might added extra features to the filter or remove some. But what you have a function here is enough to protect you against XSS.

EDIT: in addition to above function, this answer might also be relevant in part of your website protection.

Reference to the different methods:

It is also a good idea to look at following links:

And importantly it is good to be aware of Top 10 risks and learn more about it.

Daksh B

I am not sure wether this is the right approach to the problem or not but I found a function "htmlspecialchars_decode()" in the php manual. The manual says it does exactly the opposite of "htmlspecialchars()". I tried and it works well.

The html_entity_decode() function is the opposite of htmlentities().

Mark_1

I have used HTMLPurifier which is a PHP filter library to sanitize HTML input.

Sanitize POST content with HTML contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!