Friday 6 September 2013

Rails Captcha Plugin Usage


     CAPTCHA stands for Completely Automated Public Turing Test to Tell Computers and Humans Apart. Captcha code keeps machines and humans apart. You must have seen the captcha verification while filling sign up forms, posting comments, filling surveys, placing orders, e-commerce transactions, filling feedback forms and many more. Captcha verification is the way to save the website from Spam attack,malicious attack, computer programs used to fill the forms and other malicious activities.

Captcha code verification has the following advantages.
  • Prevention against fake comment spams which are posted on some blog.
  • Avoids thousands of sign up on free email services via bots.
  • Assures that only humans can fill the feedback and survey forms.
  • Prevents the attack of machine programs to break the passwords.

These Plugin generates the random images and random text written
over the images. Captcha plugin takes the help of MiniMagick to create
the images with text. So mini_magick should be installed on your
machine. 

If you don't have mini_magick on your system, use following
command to install mini_magick.

sudo gem install mini_magick

Captcha Plugin Installation Process.

Just run following command from application root directory.

script/plugin install git://github.com/hokam/captcha.git

Captcha Plugin Uninstallation Process.

Just run following command from application root directory to
remove captcha plugin.

script/plugin remove captcha


How to Use Captcha plugin ?


After installation process you need to add 'before_filter' callback in your controller to create captcha image before the processing of specified actions.

Suppose that you have a “new” action in your “CommentsController”
for which you want to create capthca  image, so just add 
“before_filter” at the top of your "CommentsController" for 
“new” action. 

before_filter :create_captcha, :only => [:new]

And add the following line in your form to include captcha image.

<%= image_captcha_tag %>

This tag generates an image with text, a link to change the captcha
image and a text box for the input of captcha text written over the
image.

And for validation of captcha code you need to call validate_captcha
helper in your validation action. Lets say in "create" action of "CommentsController" you want to validate the captcha code, so
you have to do the following modification in your "create" action.

class CommentsController << ActionController::Base
    before_filter :create_captcha, :only => [:new]
    def new
        # action specific data
    end
    # other actions
   
    def create
        if validate_captcha
            # .......
        else
            # ........
        end
    end
end

If captcha validation fails then captcha plugin set the error message
in flash[:notice] variable, so you should use flash[:notice] variable in
your form for the notification of captcha failure message.

<%= flash[:notice] if flash[:notice] %>

So by using this plugin, ruby application can be easily prevented from
the fake form fillings, thousands of sign up in a minutes via bots,
password hacking by machine programs, spam attacks and malicious
attacks.

Java Environment Setup

This post guides you on, how to download and set up Java on your machine. Java SE is freely available from the link Download Java. So you can download a version based on your operating system. Follow the instructions, specified on the website to download java and install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories.

Setting up the path for windows 2000/XP/7/8:

Assuming you have installed Java in c:\java\jdk directory:
  • Right-click on 'My Computer' and select 'Properties'.
  • Click on the 'Environment variables' button under the 'Advanced' tab.
  • Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to 'C:\WINDOWS\SYSTEM32;c:\java\jdk\bin'.
Setting up the path for windows 95/98/ME:

Assuming you have installed Java in c:\java\jdk directory:
  • Edit the 'C:\autoexec.bat' file and add the following line at the end:
    SET PATH=%PATH%; c:\java\jdk\bin
Setting up the path for Linux, UNIX, Solaris, FreeBSD:

Assuming you have installed the Java in /opt/java/jdk directory.
  • Edit the '.bashrc' file available in user home directory and add the following line at the end: 
        export PATH=/opt/java/jdk/bin:$PATH