A simple way to block spam messages in blogs _ruby topics

Source: Internet
Author: User
Tags ruby on rails

Spam message commonly known as Spam, is everywhere in the message box to send junk ads, I believe most people have seen. WordPress used to use the time, because installed Spam plug-ins, you can shield off the vast majority of Spam. But since the move from WordPress last month to the new blog platform, the first line of the day encountered a few spam, are all foreign promotional ads and what. The number of visitors to the blog itself is very small, if every day to a few Spam, too unpleasant, manual deletion is too waste of time, but also write their own small system, there is no possible plug-ins can be selected, can only find a way to solve their own.

See a lot of sites are adding a verification code to solve this problem, because Spam difficult to identify the image from the correct authentication code, but for the normal message people, but also a step out of thin air, then there is no other way to shield these Spam?

Careful analysis, the normal message should be human through the browser to submit data to our server, and Spam is usually a fixed program on the Internet scan comments form, forge data in the form, and then judge the form's action, and then POST the data to this action. In other words, the Spam sender did not normally access our form page through the browser, that is to say, the JavaScript on the page has not been implemented slightly, understand this feature, the problem will be solved.

First, we can put a hidden text box in the form that is invisible to the normal user:

<div style= "Margin:0;padding:0;display:inline" >
  <input id= "Checkspam" name= "Checkspam" type= "hidden" Value= "Hello Ruby"/>
</div>

Then write a small snippet of JavaScript code to change the value of the text box:

JQuery (document). Ready (function ($) {
  $ ("#checkspam"). Val (' ABCDEFG ');
});

Since this javascript is not executed for the Spam sender, the next thing to do is to determine the value of the hidden text box on the server, and if it is not the value of JavaScript changes, then this message is definitely Spam (excluding JavaScript disabled for normal user browsers).

Here's an example of Ruby on Rails application:

If params[:checkspam] = = "ABCDEFG"
 @comment. Save
Else
 # It ' s a spam
... End

In fact, this hidden text box is the same as the verification code, except that the code is automatically entered by JavaScript.

This method has been used for more than a month and there has been no Spam.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.