dongxie559554 2016-03-17 14: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 15: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.

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

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退