【问题标题】:Bad Word Filter, how to combine with URL ReplaceBad Word Filter,如何与 URL Replace 结合使用
【发布时间】:2010-02-25 00:25:48
【问题描述】:

我有两个脚本 - javascript 和 php..

这会清理网址

    <script type="text/javascript">
$(document).ready(function() {
    $('.search-form').submit(function() {
        window.location.href = "/file_"+ $('.search-form input:text').val() + ".html";
     return false;
    });
});
</script>

这是坏词过滤器

<?php
    if (isset($_GET['search']))
    {
    $search=$_GET['search'];

    if(is_array($badwords) && sizeof($badwords) >0)
    {
    foreach($badwords as $theword)
    $search = ereg_replace($theword,"haha",$search);
    }
    $search=preg_replace("/\s+/"," ",$search);

    $keyword = str_replace(" ", "+", $search);
    }

    else
    {
    $keyword = str_replace(" ", "+a", $keyword);
    }
    ?> 

如何将这两个脚本结合起来,将 url 中的坏词替换为“haha”?

【问题讨论】:

  • 请注意:糟糕的语言过滤器通常是一个坏主意,因为它们经常破坏有效的使用,而那些试图绕过它们的人可以简单地转向 1337 说话并使用特殊字符来获取信息穿过。仅举两个例子:assignment 和 ashita.org,因为它们都包含经常被标记的词,所以你会得到 haha​​ignment 和 ahahaa.org 之类的东西。
  • 等到有人问起 Pen Island :O

标签: php jquery filter replace word


【解决方案1】:

你可以在 PHP 中重定向

一、形式:

<form action="somefile.php">
<input type="text" id="search" name="search" value="" placeholder="Enter here..." />
<button>Search</button>
</form>

第二:

// somefile.php
  if (isset($_GET['search'])){
    $search=$_GET['search'];
    if(count($badwords)){
    foreach($badwords as $theword)
      $search = ereg_replace($theword,"haha",$search);
    }
    $search=preg_replace("/\s+/"," ",$search);
    $keyword = str_replace(" ", "+", $search);
  } else {
    $keyword = str_replace(" ", "+a", $keyword);
  }
  // here you can do any checks with the search and redirect to anywhere
  if (strlen($keyword)){
    header("location: /file_{$keyword}.html");
  }

或者你可以使用ajax来检查和清理关键字:

<script type="text/javascript">
$(document).ready(function() {
  $('.search-form').submit(function() {
    $.ajax({ type: "POST", dataType: "HTML",
             url: "clean.php", 
             data: { search: $('.search-form input:text').val()},
             success: function(response){
               if (response.length > 0) {
                 window.location.href = "/" + response;
               }
             }
   });
</script>

清理.php:

  if (isset($_GET['search'])){
    $search=$_GET['search'];
    if(count($badwords)){
    foreach($badwords as $theword)
      $search = ereg_replace($theword,"haha",$search);
    }
    $search=preg_replace("/\s+/"," ",$search);
    $keyword = str_replace(" ", "+", $search);
  } else {
    $keyword = str_replace(" ", "+a", $keyword);
  }
  // here you can do any checks with the search and redirect to anywhere
  if (strlen($keyword)){
    echo("file_{$keyword}.html");
  } ?>

您可以在以下位置查找有关 ajax/post/get (jQuery) 的更多信息:

http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.post/
http://api.jquery.com/jquery.get/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多