Published:
If you are having spam or censorship problems with your forms then you may want to set up a 'ban list' to detect when certain words are used.
You can do this using a Custom Serverside Validation action in the On Submit event. Here are a couple of ways to set this up.
Check for banned words
This code will check for banned words (from the array at the start of the code) and show a Form error if one is used.
<?php
$ban_array = array(
'seo',
'search engine',
'experts'
);
$isvalid = true;
foreach ( $ban_array as $v ) {
if ( strpos($form->data['input_textarea_1'], $v) !== false ) {
$form->validation_errors['input_textarea_1'] = 'Banned words in use!!';
return false;
}
}
?>
Note: this is checking the text area named 'input_textarea_1', change this name to match your textarea or text input name.
If you want to check more than one input then you can use this extended version:
<?php
$ban_array = array(
'seo',
'search engine',
'experts'
);
$input_array = array (
'input_textarea_1',
'input_text_2'
);
$isvalid = true;
foreach ( $ban_array as $v ) {
foreach ( $input_array as $vv ) {
if ( strpos($form->data[$vv], $v) !== false ) {
$form->validation_errors[$vv] = 'Banned words in use!!';
return false;
}
}
}
?>
Both of these filters will stop as soon as the first example of a banned word is found.
What action you choose to take will depend on your form and the kind of filtering you want to do.
- You can return the user to the form and display an error message by using the usually Evetn Loop action in the On Fail event of the Validation action.
- You could use a ReDirect User action and send the user to some other page on your site, or elsewhere.
- You could use Custom Code and $mainframe->redirect('url', 'message'); to redirect elsewhere on your site and display a system message
- You could use a DB Save action to log the problem and the user id.
- You could use a Custom Code action to block the user or de-register them.
Censoring words
If you are OK to accept the post but want to remove banned words from the result then you can try a version of this code in a Custom Code action in the On Submit event.
<?php
$ban_array = array(
'xxx' => '***',
'coach => '*##*',
'gucci' => '*!!*',
'thomas sabo' => ''
);
$input_array = array(
'input_textarea_0'
);
foreach ( $input_array as $in ) {
foreach ( $ban_array as $k => $v ) {
$form->data[$in] = str_ireplace($k, $v, $form->data[$in]);
}
}
?>
While this was written as a 'censor' you could also adapt it to apply almost any kind of substitution or formatting to your form data.

Comments: