PHP安全性(strip_tags,htmlentities)

Tia*_*ago 5 php security stripslashes strip-tags

我正在从事一个个人项目,除了使用准备好的语句之外,我还希望将每个输入用作威胁.为此,我做了一个简单的功能.

function clean($input){
if (is_array($input)){
    foreach ($input as $key => $val){
        $output[$key] = clean($val);
    }
}else{
    $output = (string) $input;
    if (get_magic_quotes_gpc()){
        $output = stripslashes($output);
    }
    $output = htmlentities($output, ENT_QUOTES, 'UTF-8');

}
return $output;
}
Run Code Online (Sandbox Code Playgroud)

这是否应该用于以下代码?

        $output = mysqli_real_escape_string($base, $input);
        $output = strip_tags($output);
Run Code Online (Sandbox Code Playgroud)

对不起,这可能是一个愚蠢的问题,但我想避免与我的代码有任何问题:)谢谢你的帮助

Ant*_*dge 5

我赞赏你的努力.友好的社区成员必须考虑将您的业务脱钩.

1)有一个功能/例程/类/过滤输入法(filter_input_array(),strip_tags(),str_ireplace(),trim(),等等).您可能希望创建使用循环进行过滤的函数.诸如双重编码,一次性条带欺骗等技巧可能会破坏单一用途strip_tags().

这是strip_tags()Sanitizer班级的包装方法.请注意它如何将旧值与新值进行比较以查看它们是否相等.如果它们不相等,它会继续使用strip_tags().虽然,在执行此方法之前,已经完成了一些初步的INPUT_POST/$ _POST检查.此使用的另一个版本trim()实际上是在此之前执行的.

private function removeHtml(&$value)
{    
    if (is_scalar($value)) {
        do {
            $old = $value;
            $value = strip_tags($value);

            if ($value === $old) {
                break;
            }
        } while(1);
    } else if (is_array($value) && !empty($value)) {
        foreach ($value as $field => &$string) {
            do {
                $old = $string;
                $string = strip_tags($string);

                if ($string === $old) {
                    break;
                }
            } while (1);
        }
    } else {
       throw new Exception('The data being HTML sanitized is neither scalar nor in an array.');
    }

    return;
}
Run Code Online (Sandbox Code Playgroud)

2)有另外一个用于验证输入(filter_var_array(),preg_match(),mb_strlen,等...)

然后,当您的数据需要切换上下文时......

A)对于数据库,使用预备语句(PDO最好是).

B)用于将用户输入返回/发送到浏览器,使用htmlentities()htmlspecialchars相应地转义输出.

就魔术引语而言,最好的办法就是禁用它php.ini.

现在,由于各种构造都有自己的职责范围,您所要做的就是管理处理程序文件中的逻辑和数据流.这包括向用户提供错误消息(必要时)和处理错误/异常.

如果数据直接从HTML表单进入数据库,则无需使用htmlentities()htmlspecialchars立即使用.转义数据的目的是防止它在新上下文中被解释为可执行指令.将数据传递给SQL查询引擎时没有危险htmlentities()htmlspecialchars可以解决(这就是为什么要过滤和验证输入,并使用(PDO)预处理语句).

但是,数据公布后从检索数据库表直接送往了浏览器,好了,现在使用htmlentities()htmlspecialchars.创建一个function使用forforeach循环来处理该场景的.

这是我Escaper班上的一个片段

public function superHtmlSpecialChars($html)
{
     return htmlspecialchars($html, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);
}

public function superHtmlEntities(&$html)
{
    $html = htmlentities($html, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);
}

public function htmlSpecialCharsArray(array &$html)
{       
    foreach ($html as &$value) {
        $value = $this->superHtmlSpecialChars($value);
    }

    unset($value);
}

public function htmlEntitiesArray(array &$html)
{       
    foreach ($html as &$value) {
        $this->superHtmlEntities($value);
    }

    unset($value);
}
Run Code Online (Sandbox Code Playgroud)

您必须根据自己的个人品味和情况定制代码.

注意,如果您计划在将数据发送到浏览器之前处理数据,请先进行处理,然后使用方便的花花公子htmlentities()htmlspecialchars循环函数进行转义.

你能行的!