PHP Developers Network

How To Check For Banned Word On Page With Javascript ?
Page 1 of 1

Author:  UniqueIdeaMan [ Wed Oct 04, 2017 7:23 pm ]
Post subject:  How To Check For Banned Word On Page With Javascript ?


I'm trying to add a content filter (banned words filter) onto a web proxy. When the Javascript detects any of the banned words on the page, it should:

* As soon as a banned word is detected, it should not check for any more but immediately echo/print on screen/display a warning to the user with the first banned word spotted:
"Banned word "blah" found on page. You will be redirected to Google".
* Then, redirect to Google.
* Dump to database the banned word found, and the url of the page the banned word was found on. Eg.

Banned Word Found|Url|Date & Time of the Server
ass||25-01-2007, 03:00:00

The only requirement is that, I should be able to feed a list of banned words it should check for.

What would the code be to do all that ?
I'd like to code see samples.
I'm trying to build one in php but the php gurus say it is best the banned words checking is done on client-side. Else, the page would take too long to load (high cpu usage) if the checking is done on server side. I have no experience in writing Javascript code.

Thank You!

Author:  Celauran [ Thu Oct 05, 2017 4:58 am ]
Post subject:  Re: How To Check For Banned Word On Page With Javascript ?

What have you tried so far? Where have you encountered issues? Have you looked at ?

You're obviously going to have to learn JS before being able to implement this, but you'll need to learn that anyhow. Do you know JS at all?

Author:  UniqueIdeaMan [ Thu Oct 05, 2017 5:57 am ]
Post subject:  Re: How To Check For Banned Word On Page With Javascript ?

You see, I want to run a public web proxy but I do not want my users viewing sites containing banned words. If I have 1k banned word and a page contains 1k words then my current php filter has to loop 1k*1k=1m times on a single page to check for banned words. Now, imagine the cpu usage. My host likely to object. On top of that, when thousands of users simultaneously use my web proxy then you can imagine how slow my service would become to load pages.
That is why at Stack Over Flow the php guru who is helping me build the php filter, suggests I look into the JS filter instead.

I have now been told that, many people surf with java and javascript disabled. Nothing would run with these surfers (web proxies). Plus, anytime I do something client side, it can be faked. In other words they could remove all banned words on the fly before my javascript ran. So a page with a banned word would appear to you as fine!
This is shocking news for me indeed! Client-side can be faked ? How ? You JS experts care to elaborate more ? I have to learn this loophole to prevent maliciousness in the future.
So, what is the solution then ? I mean, the php filter would be too slow and the page would load slowly and my users would get fedup and ditch the free service. What would you do if you were in my position ?

I do not know JS atall. I'm struggling with php. Can't take 2 giants all at once. If I can find a ready made script online then using my common sense I maybe able to customise it according to my needs asking a little here and there on many forums and reading a little tutorials (but I do not have the time, nor the patience to learn the full language) then I'll share the final version on all forums. Thus, I contribute back to the forums that helped me a little.
I'll google and see if I can find any free samples. I might find a few on Stack Over Flow.

You know my php project is nearly finished. I added a link click logger onto a free web proxy to log what users are browsing (with their permission). I wanted to add a words filter (banned words) so users don't view forbidden sites nor my proxy logs them. However, the php forums failed to advise me that the filtering is best done at the client side rather than server side. Else, the service will become slow. I learnt this at Stack Over Flow. Even though my php words filter is finished. I reckon the JS word filter would be best for my project.

The owner of this fiddle just brought it to my attention that she has written a fiddle and to check it out:

Or, I can use the jQuery library she says:

Frankly, I do not know what a fiddle is. Checking it out now.
Neither I have a single bit of experience with jQuery lib.

I hope it does the job. You experts are welcome to check and give your opinions as feed-back to me and all the future newbies finding their way here. :)

Author:  UniqueIdeaMan [ Thu Oct 05, 2017 6:15 am ]
Post subject:  Re: How To Check For Banned Word On Page With Javascript ?

Anyone know any solution ?

Author:  UniqueIdeaMan [ Thu Oct 05, 2017 5:02 pm ]
Post subject:  Re: How To Check For Banned Word On Page With Javascript ?


Why do you reckon this code does not work ? I only see a blank page.
The code will need to run everytime cURL loads a page so that the JS can check the page for banned words.

Syntax: [ Download ] [ Hide ]

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, '');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );

$result = curl_exec($curl);
if (curl_errno($curl)) {
    echo 'Error:' . curl_error($curl);
$response = curl_getinfo( $curl );
if($response['http_code'] == '200' )
                var pageData = "hello <a>example can have tags! swear word is </a><div> one..</div>";
   if(checkSwear(pageData)== false){
   //redirect to google

function checkSwear(sentance) {
        var swear_words_arr=new Array("blow", "nut", "smurf");
  var regex = new RegExp('\\b(' + swear_words_arr.join('|') + ')\\b', 'i' );
  if(regex.test(sentance)) {
    alert("Please refrain from using offensive words"); /* + alert_text */
    return false;
  } else {
        return true;

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group