Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php validation to block spam

spam email contact form php



10:03 pm on Dec 24, 2008 (gmt 0)

5+ Year Member

I have a contact form set up with php. I'd like to block some robots or users that have been attempting to spam the form. If I create an if statement to block mulitple key words what would it look like? I can get the form to block one word but I'm unsure how to form the if statement to block more than one word:

if ($msg=="insert bad words") {
$error_msg[]='<strong>Go Away!</strong>';

do I separte my target keywords to block with commas or quotes? I've tried everything.


11:15 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Forget about blocking users, too many proxies and dynamic IP

what you need is a clear head :) and review both links and each and every links within both links
Did I already confuse you?

First set [webmasterworld.com]

Second set [webmasterworld.com]


6:24 am on Dec 25, 2008 (gmt 0)

5+ Year Member


Is it not a good idea use captcha to avoiod spam?



4:30 am on Dec 26, 2008 (gmt 0)

5+ Year Member

henry0 - yep you got me confused.

Mahabub - how come?


2:04 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Blocking bad words can help to keep your form submissions / forums clean, but IMO the majority of spammers posting these bad words are likely to be robots and there are better techniques to try and stop robots, rather than targeting the bad words (there are a lot of bad words!).

Another thread that might be a useful read... Frustrated with Spambots Coming In through Webmail Forms [webmasterworld.com]

Anyway, back to your original query of checking for the existence of any one of a list of words within a block of text. I'm not sure that this can be done within a simple IF statement unless a strpos()-like function exists that can take an array of strings/words to match (which I don't think it can)?

One way to do this is to store your list of bad words in an array, then step through this array searching for each word in your $msg. As soon as you find a $bad_word then fail...

$msg = 'This is my message containing a nasty word.'; 
$bad_words = array('bad','nasty','awful','rude','words');
$found_bad_word = false;
foreach ($bad_words as $word) {
if (strpos($msg,$word) !== false) {
$found_bad_word = true;
if ($found_bad_word) {
echo '<p>Found a bad word: "'.$word.'"</p>';
} else {
echo '<p>No bad words found :)</p>';

As mentioned, I wouldn't use this bad words list to block a user (unless perhaps they persistently offend), but to perhaps warn that they have entered unsavoury content that should be corrected.


2:14 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

As per penders, bad words should only be used to clean txt
Banning members does not work perfectly due to proxy and dynamic IP
Captcha is only a better level of defense; be advised that some bots are going through it as butter

penders and I supplied a few links
you should invest time to implement such a system
one of the bots expert on board is alex, review, learn from the posts and seek help when needed.


2:01 pm on Dec 28, 2008 (gmt 0)

5+ Year Member

Thanks for the info everyone, I'll take some time and read through those threads.

Featured Threads

Hot Threads This Week

Hot Threads This Month