2014-01-31 49 views
0

我想从我的搜索框阻止某些搜索特定的搜索词....地址看起来像下面这样:如何拦截在我的网站从我的搜索形式

http://domain.com/search.php?search=dog

我该怎么办设置一个数组或阻止搜索词“狗”的东西?

<form id="headbar-search" action="search.php" method="GET" x-webkit-speech="x-webkit-speech"> 
<input type="text" name="search" id="jsid-search-input" value="<?php echo$_GET['search']; ?>" class="ui-autocomplete-input search search_input" placeholder="Search&#8230;" tabindex="1"/> 
<div class="ui-widget"></div>    
</form> 

的search.php

<?php include 'header.php'; ?> 

<!-- Container --> 
<div id="container"> 
<div class="left"> 
<div class="pic" style="overflow:hidden; margin-top:5px;"> 
<div style="margin:0 0 5px 0;overflow:hidden;border:none;height:auto;">   
<div class="video-container" style="text-align: left; background: #fff;"> 
<h2>Search results for: <?php echo htmlspecialchars($_GET['search'], ENT_QUOTES, 'UTF-8'); ?></h2> 
________________________________________________________________________________________ 
<br><br> 
<?php 
if($svid) { 
$squerys = mysql_query("SELECT * FROM videos WHERE title LIKE '%$word%' OR author LIKE '%$word%'"); 
while($ft = mysql_fetch_array($squerys)){ 
?> 
<table> 
<tr> 
<td> 
<?php if($st['seo'] == 1) { ?> 
<a href="./<?php echo seo($ft['title'], $ft['id']); ?>.html"> 
<?php }else{ ?> 
<a href="./?v=<?php echo$ft['id'];?>"> 
<?php } ?> 
<div class="image-container"><img src="<?php echo$ft['thumb_large']; ?>" width="120" height="130"></div></a></td> 
<td style="padding-left:10px; vertical-align:top;"><h3> 
<?php if($st['seo'] == 1) { ?> 
<a href="./<?php echo seo($ft['title'], $ft['id']); ?>.html"> 
<?php }else{ ?> 
<a href="./?v=<?php echo$ft['id'];?>"> 
<?php } ?> 
<?php echo stripslashes($ft['title']); ?></a></h3>By: <?php echo$ft['author']; ?><br><?php echo number_format(round($ft['views'])); ?> views</td> 
</tr> 
<br> 
</table> 
<?php 
} 
}else{ 
echo"No results found."; 
} 

?> 

</div> 
<div style="margin: 5px 0 0 0;"></div> 

</div></div> 


</div> 

<?php include 'sidebar.php'; ?> 


<div class="clear"></div> 

</div> 
<!-- Container --> 

<?php include 'footer.php'; ?> 
+0

我们可以看看的search.php漂亮吗? – bastienbot

+1

做它的服务器端。 – pleasedontbelong

+0

@bastienbot更新... – user2405912

回答

1

一个非常快的标记

$Naughty_Words = array ("Cat","dog"); 


function DetermineBanned ($Input,$Array){ 
    if (in_array($Input,$Array)){ 
     return true; 
    } 
    return false; 
} 


var_dump(DetermineBanned("Cat",$Naughty_Words)); // Returns bool(true) 
var_dump(DetermineBanned("dd",$Naughty_Words)); // Returns bool(false) 
var_dump(DetermineBanned("CatMan",$Naughty_Words)); // Returns bool(false) 
var_dump(DetermineBanned("CatMan",$Naughty_Words)); // bool(false) 
var_dump(DetermineBanned("CatDog",$Naughty_Words)); // bool(false) 
    /// Due to the lack of initial code being shown in your question, I have spotted a bug, it's up to you to decide how to make it fool proof 




//Validate with 

if (DetermineBanned($_GET['Key'],$Naughty_Words) !== false){ 
    echo "Banned Words Detected"; 
    exit; 

} 
+0

所以我可以添加此JavaScript到我的网页,它不会搜索单词猫和狗? – user2405912

+0

我需要把这个代码放在哪里? @Daryl Gill – user2405912

1

它在search.php中的文件来完成。

例子:

$excluded_words = array("dog", "hello", "world"); 
if(in_array($_GET['search'], $excluded_words) { 
    echo "Keyword not found"; 
} 

注:这只是你传递一个字只有

+0

我需要做的就是将这个添加到我的搜索PHP? @Andresito – user2405912

+0

在查询数据库之前执行此操作,并且在用户搜索排除的单词时显示消息并使用exit()终止php执行。 – Andresito

1

把你的HTML和PHP之间的一些JavaScript。

<form> 
    <input type="text" id="search-box" value="" placeholder="Search&#8230;"/> 
    <input type="submit" onclick="search()"/>   
</form> 

的JavaScript

var badWords = ["dog"]; 
function search() { 
    var searchStr = document.getElementById("search-box").value; 
    var badWordHit = false; 
    for (key in badWords) { 
     if (badWords[key] == searchStr) { 
      badWordHit = true; 
     } 
    } 
    if (badWordHit) { 
     alert("I ain't searchin' for no dog"); 
    } else { 
     alert("searching"); 
     // do ajax request here 
    }  
}