PHP关键字日志脚本中的单词块列表

PHP关键字日志脚本中的单词块列表,php,mysql,sql,logging,Php,Mysql,Sql,Logging,我有一个PHP脚本,它将搜索脚本上的查询记录到MySQL数据库中。但是,如果为不合适的术语输入查询,我不希望将其添加到数据库中。我想要一个我认为不合适的词的列表,在将这个词添加到数据库之前检查它。我该怎么做呢 我当前的PHP代码是: <?php $query=$_GET['q']; logQuery($query); function logQuery($query){ $query="insert into queries (query) values ('$query') on d

我有一个PHP脚本,它将搜索脚本上的查询记录到MySQL数据库中。但是,如果为不合适的术语输入查询,我不希望将其添加到数据库中。我想要一个我认为不合适的词的列表,在将这个词添加到数据库之前检查它。我该怎么做呢

我当前的PHP代码是:

<?php

$query=$_GET['q'];

logQuery($query);
function logQuery($query){
$query="insert into queries (query) values ('$query') on duplicate key update value=value+1";
mysql_query($query);
}

?>

您只需准备一个数组中的坏单词列表,并使用以下代码将这些单词替换为*

$badWords = array("badWord1", "badWord2", "badWord3", "badWord4", "badWord1");
$cleanUp = str_replace($badWords, "****", $originalString);
如果您想识别这些词,包括其扩展名(如ing、ed、s、ly等),并回复一条消息,说明其不合适,您可以执行以下操作。任何你应该如何得到这样的坏词的完整列表来识别它

// Array of Bad words
$words = array('badWord1','badWord2','badWord3','badWord14');
// Array of extention to words
$exten = array('','ed','ing','s');
// Input string
$str = 'this is a dam sentence';
// Create an array from input
$string = explode(' ',strtolower($str));
// Create a new array for all combinations of swear words
$wordList = array();
// Add all combinations to the new array
foreach($words as $word){
   foreach($exten as $ext){
      $wordList[] = $word.$ext;
   }
}
// Loop through each input word, and check if it is a bad word or not
// FALSE = Good Words
// TRUE = Bad Words
$badWord = FALSE;
foreach($string as $s){
   if(in_array($s, $wordList)){
      $badWord = TRUE;
   }
}
// Do something if output is good or bad in this case display a message
if($badWord)
   echo 'This input contains inappropriate content';
else
   echo 'This is valid input!';

出于好奇,你说的“不合适”是什么意思?脚本用户可以通过使用带撇号的查询在数据库上运行任意SQL。这就是所谓的SQL注入。
// Array of Bad words
$words = array('badWord1','badWord2','badWord3','badWord14');
// Array of extention to words
$exten = array('','ed','ing','s');
// Input string
$str = 'this is a dam sentence';
// Create an array from input
$string = explode(' ',strtolower($str));
// Create a new array for all combinations of swear words
$wordList = array();
// Add all combinations to the new array
foreach($words as $word){
   foreach($exten as $ext){
      $wordList[] = $word.$ext;
   }
}
// Loop through each input word, and check if it is a bad word or not
// FALSE = Good Words
// TRUE = Bad Words
$badWord = FALSE;
foreach($string as $s){
   if(in_array($s, $wordList)){
      $badWord = TRUE;
   }
}
// Do something if output is good or bad in this case display a message
if($badWord)
   echo 'This input contains inappropriate content';
else
   echo 'This is valid input!';