SMF Help

SMF Support => Scripts and Tutorials => Topic started by: admin on July 13, 2017, 11:57:25 AM

Title: How to block links in the subject line of posts!
Post by: admin on July 13, 2017, 11:57:25 AM
Spammers deserve a special place in *. They register with random pseudo-names, post one or two spam messages and annoy everyone on the forum. One of the methods they use is to post links to their garbage websites in the subject of the post; that way, the link is prominently displayed in the list of posts.

Here's a simple trick to block links in the subject of a post:

First, open /Themes/default/languages/Errors.english.php and add this at the end (or anywhere):

Add:
Code: [Select]
$txt['error_bad_subject'] = 'You are not allowed to use that subject line.';

Now open /Sources/Post.php

Find:
Code: [Select]
	// You are not!
if (isset($_POST['message']) && strtolower($_POST['message']) == 'i am the administrator.' && !$user_info['is_admin'])
fatal_error('Knave! Masquerader! Charlatan!', false);

Add this after:
Code: [Select]
    // HTTP not allowed!
    if (substr_count(strtolower($_POST['subject']),"http://") > 0 || substr_count(strtolower($_POST['subject']),"https://") > 0)
        {
       $post_errors[] = 'bad_subject';
        }

If you want to also block www. in the subject, use this instead of the above code:
Code: [Select]
// http not allowed!
    if (substr_count(strtolower($_POST['subject']),"http://") > 0 || substr_count(strtolower($_POST['subject']),"https://") > 0 || substr_count(strtolower($_POST['subject']),"www.") > 0)
        {
       $post_errors[] = 'bad_subject';
        }

Now, when they try to post a message with a URL in the subject they will be greeted with a nasty error message:

(http://www.smfhelp.com/photo/spam_url_test.jpg)


Feel free to change the error message text to whatever you want and add translations to other language files you use on your forum.   ;)