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 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置