dongxie559554 2016-03-17 06:27
浏览 30
已采纳

字符串中的PHP黑名单单词

I have a piece of Javascript code that generates a dynamic XML string. This XML string is then passed to a PHP file where I need to check to make sure the string doesn't contain any bad words that could allow for SQL injection.

I figured I would just create a blacklist and if any word was found, we just don't send the XML to the database.

My snippet of code however isn't returning true when I pass in one or more of the blacklist words.

// Create a blacklist array
$blacklist = Array('create', 'alter', 'update', 'delete', 'drop', 'insert', 'into', 'from', 'where');

// Define our vars
$xml = '<blah>alert table drop something create</blah>';
$actor = $_COOKIE['QID'];
$sp = $_POST['sp'];

// Lets check the XML string to see if it contains any database altering words
function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($a,$str) !== false) return true;
    }
    return false;
}

// Check our XML string
if(contains($xml, $blacklist))
{
    echo 'Contains';
}
else
{
    echo 'Does not contain';
}

Is there a better way to handle this type of check? I wasn't sure what to search for so figured the blacklist of words would be sufficient.

展开全部

  • 写回答

1条回答 默认 最新

  • drrkgbm6851 2016-03-17 07:09
    关注

    You have the parameters in the wrong order when calling stripos. Instead of stripos($a,$str), you want stripos($str,$a). The first version is search for the entire XML string within an individual "bad" word. The second searches for the word within the XML string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部