Spamming? What Spamming??
These days, whether you are blogging or running a business website, form submission spamming has become on of the primary issue of concern and frustration. Form submission spamming usually happens if you have commenting feature on your website. Spammers like to use this opportunity to promote their website and most importantly hoping to get higher rank in search engines. Im sure most of web developers/webmasters has gone through nightmare(s) when you found dozens of v1agra (you know what i mean) related links floating around your website posts.
Form submission some times occurs on your membership registration form as well, people attempting to create multiple accounts instantly. And if you have content submission features, such as Digg, Reddit or other social network relying on user submitted contents, then you have another form submissions to worry about.
Prevention is better then cure.
In deed it is. There are many ways you can prevent most of these spammers. It is hard to stop hackers entirely, but these methods should make attackers life bit difficult and have them find another nest to play around with.
I have prepared to write about 3 ways (out of many) you can prevent form submission spam. You can implement them individually or combine them with any combination you want to add even greater security based on your need. These 3 methods are:
1. Post Hash Authentication
2. Hidden Text field Mechanism
3. CAPTCHA
[Please note, all these are programming language independent concepts, but I will be using php to demonstrate some code when necessary.]
POST HASH AUTHENTICATION:
This method relies on renewed hash code for authentication. So every time your page is loaded, a unique hash code is created by your server and put it on session value, which then need to be passed by form on every submission. Therefore, upon form submission the server can verify the passed hash code with session hash code for validity. And the trick is that, after validating, server has to destroy the hash right away and start looking for another one, a different one. This is very easy to implement.
//check if form submission occurred
if((count($_POST)) {
if(!isset($_POST['posthash'] && ($_SESSION['posthash'] != $_POST['posthash'])) {
die('unauthorized request.');
} else {
$_SESSION['posthash'] = ""; // removing current code
}
}
// right after check we want to regenerate post hash, regardless if form was submitted or not
// actual hash value can be simply md5(current time in nano second)
define("POSTHASH", md5(time())); // making it constant for global access across your web application.
$_SESSION['POSTHASH'];
These codes should be on the top of each page (save it in separate file and include in every page).
In your HTML form, you need to add special hidden field and supply this code
<form ...>
...
<input type='hidden' name='posthash' value='<?php echo POSTHASH ?>' />
</form>
This method will also prevent users from spamming by refreshing browser for multiple submission, since POST variable will hold previous hash and therefore will be rejected.
Hidden Text field Mechanism:
This method is based on a blog that I came up across. Concept is very cheap, but extremely powerful to fool most of spam bots (no bender, it's not fambot).
This is how it works. You simply add a text field in your form and hide it's "visibility" from the users. The idea is that users will not see the text field there for it will stay blank. But, most of the spamming bots attempts to fill all the text fields they find in the form before submitting. So a simple conditional check, whether or not hidden text field has value or not, can give you clue if this is a spamming or not.
<form ...>
...
<div class='special-field'><label>Enter your Middle name:</label> <input type='text' name='middlename' value='' /></div>
...
</form>
In your CSS code, you need to set 'special-field' display to 'none'. This way users will not see the label and text field, but still will be visible in the HTML code which is used by spammers.
In your server script, all you have to do is check if 'middlename' field has value or not:
if(!empty($_POST['middlename'])) {
die('unauthorized request');
}
[read original blog: http://www.rustylime.com/show_article.php?id=338 ]
CAPTCHA:
This method is used most widely and most effective way of preventing users from spamming. CAPTCHA is almost like POST HASH concept, only addition is that instead of putting the hash value (or the captcha code) in hidden field automatically, a human user needs to read it of distorted image and enter it him/herself. Therefore this gives you highest level of prevention from Spamming.
There are many free and effective CAPTCHA codes and class files with tutorials floating around the web, so I will not go in detail here.
Lots of developers does not like to include CAPTCHA mechanism because it puts little bit burden on genuine users and can discourage them from posting comment or even registering.
Conclusion:
There are many ways one can attempt to prevent from form submission. These three I found to be useful. However, as I mentioned earlier, ti is not possible to entirely block hackers from doing what they do best. You constantly have to modify your code, provide additional security as your site gets more and more popular.
Please share your tips and tricks, comments, suggestions.
[PS, I am only using Hidden Text field mechanism on this site, I hope some one can start spamming me so I can push laziness out and implement other once :)]