2011-06-26 7 views
-1

検索スクリプトのクエリをMySQLデータベースに記録するPHPスクリプトがあります。しかし、クエリが不適切な用語のために置かれている場合、私はそれがデータベースに追加されるのは嫌です。私は、データベースに用語を追加する前にチェックされている不適切な単語のリストを求めています。これをやり遂げるにはどうすればいいですか?PHPキーワードロギングスクリプトのWordブロックリスト

私の現在の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); 
} 

?> 
+0

「不適切な」とはどういう意味ですか? http://stackoverflow.com/questions/1835605/natural-language-processing-find-obscenities-in-english/1835666#1835666 – gbn

+0

スクリプトのユーザーは、アポストロフィを含むクエリを使用してデータベース上の任意のSQLを実行できます。これはSQLインジェクションと呼ばれます。 – Sjoerd

答えて

1
$inappropriate = array('clod', 'hipster'); 
if (!in_array($word, $inappropriate)) { 
    // Add word to database 
} 
0

あなたは単に配列に悪い単語のリストを作成し、*で、このような言葉を置き換えるために、次のコードを使用することができます。

$badWords = array("badWord1", "badWord2", "badWord3", "badWord4", "badWord1"); 
$cleanUp = str_replace($badWords, "****", $originalString); 

あなたはその拡張機能(例えばING、編、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!'; 
関連する問題