Support forum login

©2006-2012 IAAI Software

Contact Us Privacy Policy

Badwords filter required

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

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