根据搜索查询重定向用户,阻止特定单词



剧情:我正在建立一个网站,其中将索引大量视频,我想让人们通过关键字和句子搜索视频。

问题:但是众所周知,互联网上挤满了可以搜索坏单词或句子的坏人和机器人,我想将它们带到不同的页面,因此,如果查询与特定的阻止单词匹配,则应将它们重定向到特定的URL。

<?php include'func.php';
if(!empty($_GET['q'])){
if ($badword ="no")
{$url='/search/'.preg_replace("/[^A-Za-z0-9[:space:]]/","-",$_GET['q']).'';}
else
{$url='/badsearch/'.preg_replace("/[^A-Za-z0-9[:space:]]/","-",$_GET['q']).'';}
}
else{$url='/';}
header('location:'.$url.''); ?>

我想要一个这样的代码,其中$badword包含坏词,如果我能有一个广泛的匹配,我会喜欢的,所以搜索暗杀也会被重定向到/badsearch/,因为屁股将是一个坏词。

搜索和收集所有内容使我找到了答案。这个答案可能会对其他人有所帮助。如果用户输入 DoG 或 cAt,这也将起作用。这将在每个空格后搜索一个单词,因此如果您有像"我有一只坏狗"这样的查询,这将起作用。

<?php
$banned_names = array('dog', 'cat', 'mydog');
$words = explode(" ",$_GET['q']);
if(!empty($_GET['q'])){
foreach($words as $word)
{
$word = strtolower($word);
if(in_array($word, $banned_names)) 
{$url='/is/bad/'.preg_replace("/[^A-Za-z0-9[:space:]]/","-",$_GET['q']).'/1';}
else
{{$url='/isnot/bad/'.preg_replace("/[^A-Za-z0-9[:space:]]/","-",$_GET['q']).'/1';}}
}   
}
else{$url='/';}
header('location:'.$url.''); 
?>

一个简单的解决方案可能是这样的:

$user_input = $_GET['q']);
$badword_list= array("badword1", "badword2", "badword3", "badword4", ...);
if(in_array($user_input, $badword_list)) 
    $url = "something";
else
    $url = "something else"; 

或使用if (preg_match('/b(badword1|badword2|badword3|badword4)b/i',$user_input ))

相关内容

最新更新