You are here:  » Badwords filter required


Badwords filter required

Submitted by GemViper on Wed, 2010-04-28 08:58 in

Hello,

I have a concern with people searching for words I don't want my site associated with. The concern is that anything someone types in becomes the page "title" and are printed out on the search result page. While this may not seem like a big deal it does allow someone to type search engine banned words and then link to your pages with those words. Search engines would think the page is about those bad words because the title/url/text on page all have them.

Here is an example on your example site - {link saved}
Thanks in advance

Submitted by support on Wed, 2010-04-28 09:13

Hi,

This is actually quite easy to implement; but a general comment first would be to make sure that search.php is excluded in robots.txt (indeed it is excluded in the example file included with the distribution).

However, I fully appreciate that there may be circumstances in which it is desirable to have search.php permitted to well behaved robots; so to implement a bad words filter, start by making a copy of includes/stopwords.php and save it as

includes/badwords.php

Open the file, and look for the following code at line 2:

$stopwords = array(

...and REPLACE with:

$badwords = array(

Then, edit the array (start by deleting every entry that is there already of course!) to contain your bad words (all in lower case), each in quotes, separated by commas.

Now, to use the new file in search.php, look for the following code beginning at line 4:

  require("includes/stopwords.php");
  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");

...and REPLACE that with (PHP5+):

  require("includes/stopwords.php");
  require("includes/badwords.php");
  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
  $words = explode(" ",$q);
  foreach($words as $word)
  {
    if(in_array(strtolower($word),$badwords))
    {
      $q = str_ireplace($word,"",$q);
    }
  }

That will strip all bad words from $q before being used by the rest of the script.

Hope this helps!

Cheers,
David.

Submitted by GemViper on Wed, 2010-04-28 09:16

In case it helps here is a bit of code I've seen used. It's simple but I think it would cause rewrite problems and perhaps blank results for the index page and category pages with Price Tapestry. Instead of messing with the code too much I thought I'd ask someone who knows it better than me. eregi_replace is case insensitive as well.

$filter = $_GET["q"];
$bad_words = explode('|', 'bad|words|here|more|bad|words|etc');
foreach ($bad_words as $naughty)
{
$filter = eregi_replace($naughty, "Site Name", $filter);
}
if ($filter == '') // If value does not equal anything, ie no q, set the title with a default
{
$filter = "Site Name";
} else { // if value is not empty nothing happens, code moves on
;
}

Submitted by GemViper on Wed, 2010-04-28 09:18

woops, looks like we posted at the same time, I'll give your way a try.

Thanks David

Submitted by Joko on Tue, 2013-12-10 01:18

How to change badword to default query?

Submitted by support on Tue, 2013-12-10 08:10

Hello Joko,

In search.php if you look for the following code at line 6:

  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");

...and REPLACE with:

  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
  $badwords = array("bad","words","here");
  $words = explode(" ",strtolower($q));
  foreach($words as $word)
  {
    if (in_array($word,$badwords))
    {
      $q = "Default Query Here";
      break;
    }
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Joko on Tue, 2013-12-10 11:25

Hi,

so i need to add badword again?

I am using badwords list as your sample:
include badwords.php

anyway to do this?

Submitted by stonecold111 on Tue, 2013-12-10 11:26

Does it work on Wordpress?

Submitted by support on Tue, 2013-12-10 11:48

Hi joko,

To use includes/badwords.php instead, simply replace this line:

  $badwords = array("bad","words","here");

...with:

  require("includes/badwords.php");

Cheers,
David.
--
PriceTapestry.com

Submitted by support on Tue, 2013-12-10 11:51

Hi stonecold111,

For equivalent modification in WordPress, edit pto_search.php and look for the following code at line 159:

  $pto_q = (isset($pto_q)?pto_common_normalise(urldecode($pto_q),":\."):"");

...and REPLACE with:

  $pto_q = (isset($pto_q)?pto_common_normalise(urldecode($pto_q),":\."):"");
  $badwords = array("bad","words","here");
  $words = explode(" ",strtolower($pto_q));
  foreach($words as $word)
  {
    if (in_array($word,$badwords))
    {
      $pto_q = "Default Query Here";
      break;
    }
  }

Or as above, if you wished to use a new file includes/badwords.php from the associated Price Tapestry installation, then in place of:

  $badwords = array("bad","words","here");

...use:

  global $pto_config_externalPath;
  require($pto_config_externalPath."includes/badwords.php");

Cheers,
David.
--
PriceTapestry.com