- Home
- Registration
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Miscellaneous
- PHP Code Examples
- Learn PHP
- Apache:OS 10048
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- PHP and MS Access
- Authentication
- PHP App Example
- Display Image PHP
- Image Collection
- Block Spam Post
- Upload File PHP
- $_REQUEST,$_SESSION
- Site map
- Advertise here
How to filter spam posts in the phpBB2 forum
How to set email account in host control panel
![]()
![]()
![]()
![]()
![]()
The phpBB2 forum has some features that can be used to block spam user IP address and ban account etc. But if you hosted a phpBB2 forum you know that it is not enough to protect it from spam posts. I want to share with you a piece of PHP code that helps to filter out spam posts. Download PHP source code at the bottom of the page. I realize that it is not possible to stop spam posts automatically and human moderator is the best protector from spam, but You cannot waste all you life to filtering spam off you forum. I wrote a function that includes an array of "bad words" which can grow without affecting the rest of the PHP code. Each post goes through the function and if it contains at least one "bad word" the function returns positive integer or it returs zero, if no "bad words" found in the post.
There is the code:
Let us go through the code step by step:
{
$mystring=' '.$mystring;
Here I add some space character to the head of $mystring, which contain a post. Later, I use strpos function to determine a position of a "bad word" in the post. If the post starts with "bad word" the strpos would return zero. I want it to return a value that is greater than zero, that is why I added space to the post head.
Then I declare an array:
$badwords=array();
And then I assign "bad words" to array elements. I can assign as many words as I want to. If spammers get clever and misspell some words, I can update my array.
I check array size:
Then I check each element of array against the $mystring (post) contents. If post contains at leat one element of array, $pos variable will become greater the zero
for($i=0; $i< $size; $i++)
{
if($badwords[$i] != "")
{
$pos=strpos($mystring, $badwords[$i]);
if($pos > 0)
$notallow=$notallow+1;
}
}
Save this fuction as a php file. I called it notallowed_words.php
After installation of the phpBB2 board, go to phpBB2 directory where posting.php is located and place the file notallowed_words.php in the same directory. Open posting.php file and modify its code. Find the following part of code in the posting.php file:
and type there: (before prepare_post($mode...) function)
//Catch adult words. Include our file with function
include('notallowed_words.php');
$words_exist=0;
//call the notallowpost function
$words_exist=notallowpost($message);
//if message (body of the post) contains bad words replace it with the text "spam text"
if($words_exist > 0)
$message="spam text";
//Check if post subject contains bad words
$words_exist=0;
$words_exist=notallowpost($subject);
//if subject of the post contains bad words replace it with the text "spam title"
if($words_exist > 0)
$subject="spam title";
//check if subject contains a link make it not link
if($subject !="spam title")
{
$subject=str_replace("http://", "h_t_t_p://", $subject);
$subject=str_replace("www", "w_w_w", $subject);
}
//check if message contains a link make it not link
if($message !="spam text")
{
$message=str_replace("http://", "h_t_t_p://", $message);
$message=str_replace("www", "w_w_w", $message);
}
Save file and We are done. I want to disable all links in posts because outbound links decrease page ranking. There is less "cruel" method to deal with links in posts. You can insert <rel="nofollow"> in each link. Then google will not count these links. A code example for that you can find in the book "Professional Search Engine Optimization with PHP" by Jaimie Sirovich and Cristian Darie. Wiley Publishing, Inc 2007. ISBN:978-0-470-10092-9
The board will display "spam title" and "spam text" in posts where "bad words" are included. You can delete these posts completely with the following PHP code:
$osql="delete from phpbb_topics where topic_title='spam title'";
if(!mysql_db_query($dbname, $osql, $db_link))
{
echo mysql_errno() . ": ";
echo mysql_error() . "<BR>";
}
else
{
//echo "spam title deleted";
}
$xsql="delete from phpbb_posts_text where post_text='spam text'";
if(!mysql_db_query($dbname, $xsql, $db_link))
{
echo mysql_errno() . ": ";
echo mysql_error() . "
";
}
else
{
// echo "spam text deleted";
}
The post subject is stored in the phpbb_topics table in the topic_title field.
The post body is stored in the phpbb_posts_text table in the post_text field.
These two delete queries will delete all records with "spam title" subject and all record with "spam text" post body. Copy and paste this piece of code in the posting.php file after the previous piece of code which ends with line: $message=str_replace("www", "w_w_w", $message);
Did you find information useful? Send to your friend a link to this page
Please rate the tutorial
| Comments |
|---|
Web programming Tips