Chat Filters

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Chat Filters

Post by Epiales »

Okay, I'm not quite ready to start coding this, but thought I'd start asking questions about it.

I'm going to add some filtered words to go with the chat I have. What I would like to know how to do would be to allow the users to turn the filter on or off? How would I go about giving the user the option to filter the chat or not?

As stated before, the chat I have I wrote a few years back to work with socialengine, so it's a home made chat. I have no filtered words yet, but thought I'd ask about options to disable or not before I started writing the code to filter words.

Any ideas on how I could do this?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Chat Filters

Post by MikuzA »

When displaying the text for the user, you have pick up a row from users stored settings or something, where it's stored filter_chat 1|0.

If $filter_chat is 1 { parse out the text for filtering before display. }
else { do not do anything }
show_chat->

I hope this helps, or gives you an idea how you could do it :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Chat Filters

Post by Xaleph »

Filters are always a good idea, i`d secure the filter as well, children shouldn`t be allowed to put the filter on/off if their parents decided to put it on. So an additional password maybe for filter resetting?

In any case, what you are looking for is either storing a list of forbidden words and then use REGEX on the parsing side. If the filter is on, regex all incoming data, filter out the words and continue, or you could use a lexter and parser, which requires a bit more time and effort, but it will create a lot more flexibility later on. It still requires a lot of regex, but many results can be cached, so instead of performing the regex operations on the fly, you can cache the results and return them instead. Another benefit is the fact you can add special syntaxing, like, I don`t know, BB code or item markings, wrap them around, give them colors or whatever and, if you feel ambitious, you could also implement inversion for languages auto translations and the like.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Chat Filters

Post by Epiales »

Okay, well I thought I would try and work on the sending the messages first in chat and blocking those words as they send them. I came up with the following:

Code: Select all

$chatfilter = "SELECT username FROM users where username='$_SESSION[username]' AND chat_filter = '1'";
$user_query = mysqli_query($db_conx, $chatfilter);
$numrows = mysqli_num_rows($user_query);

if ($chat_filter == 1) {
     
$username = parseCleanKey(protect($_POST['username']));
$message = wordFilter(parseCleanKey(protect($_POST['message'])));
$time = time();

$sql = 'INSERT INTO chat (username, message_content, message_time) VALUES ("' . $username . '  ",
     "' . $message . '",
     ' . $time . ')';
$query = mysqli_query($db_conx, $sql); 

}else{

$username = parseCleanKey(protect($_POST['username']));
$message = parseCleanKey(protect($_POST['message']));
$time = time();

$sql = 'INSERT INTO chat (username, message_content, message_time) VALUES ("' . $username . '  ",
     "' . $message . '",
     ' . $time . ')';
$query = mysqli_query($db_conx, $sql); 
}
?>
Notice if their setting in the database is set to 1, it has a wordFilter.... else, no word filter...

But this isn't working. Whether they have 1 or 0 in the database, you can still type the words. Any ideas?

And if I take out the if/else, the chat filter works fine.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Chat Filters

Post by Winawer »

You are putting the filter counter into a variable called $numrows but you're checking against a variable called $chat_filter.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Chat Filters

Post by Epiales »

Winawer wrote:You are putting the filter counter into a variable called $numrows but you're checking against a variable called $chat_filter.
kk, my code...still can't get it to work:

Code: Select all

<?php
$i = 0;

$userchatfilter = "SELECT username FROM users WHERE username='$_SESSION[username]' AND chatfilter = '1'";
$user_query = mysqli_query($db_conx, $userchatfilter);

$i = mysqli_num_rows($user_query);

if($i > 0) {
     
$username = parseCleanKey(protect($_POST['username']));
$message = wordFilter(parseCleanKey(protect($_POST['message'])));
$time = time();

$sql = 'INSERT INTO chat (username, message_content, message_time) VALUES ("' . $username . '  ",
     "' . $message . '",
     ' . $time . ')';
$query = mysqli_query($db_conx, $sql); 

}else{

$username = parseCleanKey(protect($_POST['username']));
$message = parseCleanKey(protect($_POST['message']));
$time = time();

$sql1 = 'INSERT INTO chat (username, message_content, message_time) VALUES ("' . $username . '  ",
     "' . $message . '",
     ' . $time . ')';
$query = mysqli_query($db_conx, $sql1); 
}
 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”