PHP常用代碼 —— 阻止 SQL 注入

SQL 注入或者 SQLi 常見的攻擊網站的手段,使用下面的代碼可以幫助你防止這些工具。

function clean($input)
{
    if (is_array($input))
    {
        foreach ($input as $key => $val)
         {
            $output[$key] = clean($val);
            // $output[$key] = $this->clean($val);
        }
    }
    else
    {
        $output = (string) $input;
        // if magic quotes is on then use strip slashes
        if (get_magic_quotes_gpc()) 
        {
            $output = stripslashes($output);
        }
        // $output = strip_tags($output);
        $output = htmlentities($output, ENT_QUOTES, 'UTF-8');
    }

    return $output;
}

語法:

<?php
$text = "<script>alert(1)</script>";
$text = clean($text);
echo $text;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章