Stop automated form submission by using Image Verification

Spammers are getting clever. I know the words spammer and clever don't fit easily in the same sentence but they are employing more and more methods to send you their rubbish. One method that has been around a while is automated form submissions where a bot will automatically fill out a form on your site with junk.

Now we've all been to those sites that use image verification, such as vBulletin, where the user has to enter the characters displayed in an image before submitting the form. This helps to stop automated submissions as the bot cannot "see" the image.

This simple tutorial will show you how to beat these submissions by creating an image that will prevent the user from continuing until they enter it correctly.

Essential Ingredients

ColdFusion MX 6.1

The CFX_OpenImage tag (Free from here: http://www.kolumbus.fi/jukka.manner/cfx_openimage/)

About five minutes of your time ;-)

Preparation

Create a folder named backgrounds (This will hold the backgrounds for use in your image)

Make sure you have a folder called Images

Download and install the CFX_OpenImage tag from above

Create a file name formentry.cfm or just use your existing from page.

The code (Download entire example code)

Right then. Let’s start with a basic form page that will submit to itself:

<cfif IsDefined("form.yourName")>
   <!---Process the forum--->
<cfelse>

   <form action="imageverify.cfm" method="post" name="verify">
       Your name: <input type="text" name="yourName" size="20" /><br /><br />
       <input type="submit" value="submit" />
   </form>

</cfif>

All the above code is doing is submitting a form to itself with one text field.

The next step is to set-up our variables for creating the random image so adapt the chunk of code above so it becomes this:

<cfif IsDefined("form.yourName")>
    <!---Process the forum--->

    <!---

        *****MAKE SURE YOU HAVE READ AND COMPLETED THE STEPS BELOW FOR GENERATING YOU IMAGE!*****

    --->

    <!---

        Now that we have our generated image saved as a session and also the entry from the form,
        all we do is compare the two and make sure they match

    --->

    <cfif form.imageVerify EQ session.imageText>
         The verification was good!
    <cfelse>
         The verification failed!
    </cfif>

<cfelse>

    <!---

        First we need to set the fonts, sizes etc for our image and save them to arrays.
        You can change these values to whatever you like or add more

    --->

    <!--- // List of fonts // --->
    <cfset fonttype=ArrayNew(1)>
    <cfset fonttype
[1]="Arial">
    <cfset fonttype
[2]="Comic Sans MS">
    <cfset fonttype
[3]="Tahoma">
    <cfset fonttype
[4]="MS Sans Serif">
    <cfset fonttype
[5]="Impact">
    <cfset fonttype
[6]="Times New Roman">
    <cfset fonttype
[7]=" Georgia">
    <!--- // List of font colours // --->
    <cfset fontcolour=ArrayNew(1)>
    <cfset fontcolour
[1]="00FF66">
    <cfset fontcolour
[2]="33FFFF">
    <cfset fontcolour
[3]="CCFFCC">
    <cfset fontcolour
[4]="FFFF00">
    <cfset fontcolour
[5]="FAEBD7">
    <!--- // Font weights // --->
    <cfset fontweight=ArrayNew(1)>
    <cfset fontweight
[1]="Bold">
    <cfset fontweight
[2]=" Normal">
    <!--- // Italics // --->
    <cfset fontitalics=ArrayNew(1)>
    <cfset fontitalics
[1]="0">
    <cfset fontitalics
[2]="1">

    <!---

        To further complicate the generated image we now adjust the appearance and display

    --->

    <!--- // If you change the amount of images, alter the second digit on the following line.
                Ensure all images are named start1, start2 etc up to this number // --->

    <cfset randimgname = RandRange(1,10)>
    <!--- // If you alter the number of font faces above, change the second digit below to match // --->
    <cfset randfonttype = RandRange(1,7)>
    <!--- // If you alter the number of font colours above, change the second digit below to match // --->
    <cfset randfontcolour = RandRange(1,5)>
    <!--- // For both Bold and normal weight fonts, change the following line to 1,2 (Note: Normal fonts are sometimes difficult to read on the images) // --->
    <cfset randfontweight = RandRange(1,1)>
    <!--- // Set random selection between italics/normal // --->
    <cfset randfontitalics = RandRange(1,2)>
    <!--- // Alter numbers below to adjust the range for the random rotate (Note: Seems to increase image dimensions drastically) // --->
    <cfset randrotate = RandRange(-30,30)>
    <!--- // Alter numbers below to adjust the range for the random swirl of the background image // --->
    <cfset randswirl = RandRange(10,700)>
    <!--- // Alter numbers below to adjust the range for the random wave // --->
    <cfset randwave = RandRange(10,25)>
    <!--- // Generate Random Number and store in session variable // --->
    <cfset session.imagetext = RandRange(1000,9999)>
    <!---now we pass all of the above variables into the Custom Tag--->
    <!--- // Write Image Output //--->

    <CFX_OPENIMAGE ACTION="iml"
                DEBUGs
                NAME=
"qimage"
                FILE=
"#expandpath('backgrounds/start#randimgname#.jpg')#"
                X="80"
                Y="80"
                commands="
                     setimage or1=original
                     
##Uncomment the following line if you want swirl on the background image
                     ##swirl #randswirl#

                     texture textureImagE
                     
## setfont size,italic,underline,weight,face
                     setfont 18,fontitalics[randfontitalics],0,#fontweight[randfontweight]#,#fonttype[randfonttype]#
                     
## write some text
                     text 10,10,-1,-1,#fontcolour[randfontcolour]#,#session.imagetext#
                          
## rotate by X degrees
                     rotate #randrotate#
                     wave #randwave#
                     addnoise Uniform
                     gaussianblur 1,0.5
                     write #expandpath('images/#RandRange(1000,9999)#.jpg')#
                ">

    <form action="imageverify.cfm" method="post" name="verify">
        Your name: <input type="text" name="yourName" size="20" /><br /><br />
        <!---
                Finally we disaply the image in the form
        --->

        Please enter the characters displayed below before submitting your form:<br /><br />
        <!--- // Display Image // --->
        <cfoutput query="qimage">
            <img src="images/#getfilefrompath(img_file)#" border="0">
        </cfoutput>
        <input type="text" name="imageVerify" size="10" /><br />
        <input type=
"submit" value="submit" />

    </form>

    <!---the following lines will just help you with debugging--->
    <!--- // Comment the following lines to disable debugging // --->

    <br /><br />Debugging Output:<br /><br />
    Value: <cfoutput>#session.imagetext#<br />
    Font: #fonttype[randfonttype]#<br />
    Background: #randimgname#<br />
    Rotation: #randrotate#<br />
    Wave: #randwave#<br />
    Weight: #fontweight[randfontweight]#<br />
    Swirl: #randswirl# (Currently Disabled)<br />
    Colour: ###fontcolour[randfontcolour]#<br />
    Italics: <cfif fontitalics[randfontitalics] EQ 1>Yes<cfelse>No</cfif>
</cfoutput>

</cfif>

The way forward with this tutorial is to adjust the variables, add more backgrounds, etc to further enhance the security of the image and to further confuse those pesky spam bots!

Many thanks to William Nisbet at Fresh Look Hosting for the original concept.

Name: Philip Williams
Web: http://www.openmindhosting.com

All ColdFusion Tutorials By Author: Phil Williams
  • SE Friendly URL's
    Get the search engines to really dig deep into your site by replacing the ? and & in your dynamic URL's with /. Tricks the SE bot into thinking it's a regular folder and eats up your content!
    Author: Phil Williams
    Views: 35,617
    Posted Date: Wednesday, February 12, 2003
  • Currency Conversion using Web Services
    A very simple currency convertor that uses the latest exchange rates through the available Web Service
    Author: Phil Williams
    Views: 24,913
    Posted Date: Wednesday, April 21, 2004
  • Remote Reboots with ColdFusion MX
    This tutorial will tell you how to reboot your server without even logging in.
    Author: Phil Williams
    Views: 15,424
    Posted Date: Sunday, July 18, 2004
  • Create and email ZIP files on the fly!
    This tutorial will allow you to zip up a file or files on your server and email them to you. The whole tutorial runs to less than 20 lines of code!
    Author: Phil Williams
    Views: 18,666
    Posted Date: Thursday, October 28, 2004
  • Stop automated form submission by using Image Verification
    This tutorial will show you how to stop automatred form submissions by generating a random image that must be verified before the form is submitted.
    Author: Phil Williams
    Views: 20,101
    Posted Date: Thursday, October 20, 2005
  • Complete site encryption!
    This tutorial will show you how to encrypt an entire site by recursively working though the folder structure encrypting all the cfm files and copying any non-cfm files whilst keeping the directory structure intact.
    Author: Phil Williams
    Views: 12,218
    Posted Date: Sunday, July 16, 2006
  • Simple and Efficient RecordSet Paging
    A simple and efficient method to page through hundred or even thousands of recordset result.
    Author: Phil Williams
    Views: 5,248
    Posted Date: Thursday, January 8, 2009
  • Backup Your MySQL Database with just ColdFusion Code

    This tutorial will show you how to quickly and easily backup and restore a MySQL database using just ColdFusion scripts and avoiding having to use CFEXECUTE which is normally disallowed on shared hosting platforms.

    ColdFusion 8 has some handy tags that will provide database detail but what if you're running CF7? Normally you would have to resort to running the mysqldump command but this is restrictive if you wanted to provide your users with a method to backup their databases.


    Author: Phil Williams
    Views: 2,954
    Posted Date: Friday, July 10, 2009
Download the EasyCFM.COM Browser Toolbar!