‘No CAPTCHA reCAPTCHA’ using ColdFusion

Recently Google introduced new reCaptcha API called “No CAPTCHA reCAPTCHA” . Its a complete new design captcha system. This protects your website for spammers and robots. As per Google,

While the new reCAPTCHA API may sound simple, there is a high degree of sophistication behind that modest checkbox. CAPTCHAs have long relied on the inability of robots to solve distorted text. However, our research recently showed that today’s Artificial Intelligence technology can solve even the most difficult variant of distorted text at 99.8% accuracy. Thus distorted text, on its own, is no longer a dependable test.

Now using this new captcha system users do not need to identify a complex distorted text string to prove themselves as human being. Sometimes it becomes too much irritating when the text strings are very much distorted even difficult to recognized by human beings. As per the new catacha system users just need to click on checkbox to identify themselves as human being.

Google does say that this won’t entirely do away with CAPTCHAs — there may still be times when you’ll be asked to enter some text as well.

Google’s new reCATPTCHA system makes use of an advanced risk analysis engine to identify humans and robots.The idea here is to track user’s actions before, during and after ticking the check box saying “I’m not a robot“. Basically it checks how the cursor moved on its way to the check (organic path/acceleration), which part of the checkbox was clicked (random places, or dead on center every time), browser fingerprint, Google cookies & contents. At the end of the day, if it’s not sure, it will still offer you traditional CAPTCHA .

Here is how Google’s new reCAPTCHA looks like.


Sometime if it can not verify only by clicking the checkbox it presents with the old traditional captcha, Re

In this post I had implemented new reCaptch API system with HTML login form using ColdFusion.


1. Click Here to to create a Google reCaptcha application.


2. After this step you will be provided with google site key and secret key. The site key will be used in our HTML form as a hidden field and secret key will be used for communication between our site and google.



  1. Add the google site key to the form with a hidden field. Example,
  2. <div class="g-recaptcha" data-sitekey="6LctadUSAAAAAI81jKmWaBFGWwMky64xbBGhVl8L"></div>
  3. When the user clicks on the check box, an ajax request will be automatically sent to the server and validate the CAPTCHA, if it is valid mark it as in use. (Show the result – identifier is OK/not OK – to the user)
  4. When the user sends the form, the form’s data contains the identifier. Check it once more on the server side by making a http request to google, it should exist and it should be in in use state.
  5. If all validations are succeeded , the form’s data is ready to use/process

Here is the quick demo I have implemented using ColdFusion.


‘this.datasource’ is awesome in ColdFusion 11

Normally In ColdFusion applications we set up the datasources in ColdFusion Administrator or using the Admin API.So this makes very difficult for our Application to be portable across different ColdFusion server. Because while migrating to another ColdFusion server first of all we have to recreate all the datasources again.

(Note: Once I have tried to back up all my datasources using the neo-datasource.xml file(which stores all datasource info) of my old CF server. But that does not works because of the password encryption/decryption issues.By password encryption issues, I mean the database password that we give while creating a datasource, this password gets stored in encrypted format in our ColdFusion server.So if we migrate our neo-datasource.xml file from Old CF server to new CF server , The new CF server can not decrypt those passwords properly.)

ColdFusion 11 brings a new features for creating datasources in application level instead of messing up with CF Admin.
We can define all our datasource details in Application.cfc.

 this.datasources.dsn1 = {  "database"="regression",
                   "password"="mindfire" };
 this.datasources.dsn2 = {   "driver"="MSSQLServer", 

If you are using built in DB drivers that are shipped with ColdFusion , then just specify the driver name and the corresponding driver class will be picked up by ColdFusion. In neo-drivers.xml, you can find all the possible values for the driver.

But if you are using your own custom driver, you can use this syntax.

this.datasources.customDSN = { "driver" = "other", 
                 "url" = "jdbc:sqlserver://localhost\MSSQL2008; 
                 "username" = "sa", 
                 "password" = "password", 
                 "class" = "com.microsoft.sqlserver.jdbc.SQLServerDriver"}; 

Note: But If there is a server wide datasource with the same name exist, then application level datasource will be overridden by server level datasource.

Geocoding with Google Map API and ColdFusion

What is geocoding?

Geocoding is the process of finding associated geographic coordinates ( latitude and longitude) from other geographic data, such as street addresses, or ZIP codes (postal codes).

Note: Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

Requirement of geocoding in Web Application:

  1. Geocoding allows the quick plotting of the address on a map .

  2. To locate the nearest service center by taking the customer address as input.

  3. Used in internet advertising[ to deliver targeted advertising banners based on the location of the website visitor’s IP address. ]

  4. To provide better search results depending upon the location.

  5. In a particular company’s website, to implement functionalites like “Find a Nearest Store”,by which a user can locate the nearest store.

Available Geocoding APIs:

There are several geocoding APIs available in the market.Some of the frequently used free APIs are:

  1. Bing Maps from Mircosoft

  2. Google Map API

  3. Cloudmade Geocoding

  4. Data Science Toolkit

  5. MapQuest Geocoding API

  6. Yahoo PlaceFinder API

In terms of speed,efficiency and correctness Bing Map and Google Map APIs are considered to be the best one as they cover most of the places in the world.

In this Blog, I will give an example of Integrating Google Map API with ColdFusion to find the driving distance between any two physical addresses.

Key Points to remember while using Google Map API:

  1. The google geocoding API has certain usage limits like for normal account it is 2,500 requests per day. While for business customers it is 100,000 requests.For more information please visit: https://developers.google.com/maps/faq#usagelimits

  2. All the addresses must be in a proper format which can be easily recognized by google map API. Suggested format [Full Street Name,City,State,Zipcode]

Finding distance between two places using ColdFusion and Google Map API:

Steps to Find distance between any two physical addresses.

  1. Find the Latitude and Longitude of the two places. [Using Google Geocoding API]

  2. Find the driving distance between the two coordinates.[Using Google Distance Matrix API]

Step 1:

Find the Latitude and Longitude of the two places.

This is a simple function which will take the user address as input and will return the lattitude and longitude in a structure.

<cffunction name="findLatLong"  output="false" returntype="struct">
        <cfargument name="street" type="string" required="true" >
        <cfargument name="city" type="string" required="true">
        <cfargument name="state" type="string" required="true">
        <cfargument name="zip" type="string" required="true">
<!--- Replacing all the spaces with '+' and removing ',' --->
        <cfset VARIABLES.loc_street=trim(replacenocase(replacenocase(ARGUMENTS.street," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_city=trim(replacenocase(replacenocase(ARGUMENTS.city," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_state=trim(replacenocase(replacenocase(ARGUMENTS.state," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_zip=trim(replacenocase(replacenocase(ARGUMENTS.zip," ","+","all"),",","","all"))>
        <!---Creating an address string in proper format for google maps(Streetname,City,State,Zipcode) --->
        <cfset VARIABLES.urladdress="#loc_street#,#loc_city#,#loc_state#,#loc_zip#">
            <cfhttp result="geocode" url="http://maps.googleapis.com/maps/api/geocode/xml?address=#urladdress#&sensor=false" method="get">
                <cfhttpparam type="header" name="Content-Type" value="text/xml">
            <cfset VARIABLES.gcode = "#xmlparse(geocode.filecontent)#">
            <cfif VARIABLES.gcode.geocoderesponse.status.xmltext EQ "OK">
                <cfset VARIABLES.newlat = "#VARIABLES.gcode.geocoderesponse.result.geometry.location.lat.xmltext#">
                <cfset VARIABLES.newlon = "#VARIABLES.gcode.geocoderesponse.result.geometry.location.lng.xmltext#">
                <cfset VARIABLES.gcodefail = 0>
                <cfset VARIABLES.newlat = "0">
                <cfset VARIABLES.newlon = "0">
                <cfset VARIABLES.gcodefail = 1>
                <cfdump var="#cfcatch#">
                <cfset VARIABLES.newlat = "0">
                <cfset VARIABLES.newlon = "0">
                <cfset VARIABLES.gcodefail = 1>
        <cfset VARIABLES.locationstruct=structNew()>
        <cfset VARIABLES.locationstruct.latitude=newlat>
        <cfset VARIABLES.locationstruct.longitude=newlon>
        <cfset VARIABLES.locationstruct.gcodefail=gcodefail>
        <cfreturn VARIABLES.locationstruct>

Step 2:

Find the driving distance between the two coordinates.

This function will take the latitude and longitude of two places and will return the driving distance and time required in a structure format.

<cffunction name="googleGeoCodeify" access="remote" output="true" returntype="struct">
   <cfargument name="location1_lat" type="numeric" required="true">
   <cfargument name="location1_long" type="numeric" required="true">
   <cfargument name="location2_lat" type="numeric" required="true">
   <cfargument name="location2_long" type="numeric" required="true">
            <cfset VARIABLES.drivingdetails = structnew()>
            <!--- build querystring to send to google --->
            <cfset VARIABLES.googlequerystring="origins=#ARGUMENTS.location1_lat#,#ARGUMENTS.location1_long#">
            <cfset VARIABLES.googlequerystring=googlequerystring & "&destinations=#ARGUMENTS.location2_lat#,#ARGUMENTS.location2_long#">
            <cfset VARIABLES.googlequerystring=left(googlequerystring,len(googlequerystring)-1)>
            <cfset VARIABLES.googlequerystring=googlequerystring & "&mode=driving&language=en&avoid=tolls&units=imperial&sensor=false">
            <cfset VARIABLES.googleurl="http://maps.googleapis.com/maps/api/distancematrix/json?#googlequerystring#">
            <cfhttp url="#VARIABLES.googleurl#" method="GET" throwonerror="no" timeout="10"/>
            <cfset VARIABLES.resultstr = deserializeJSON(cfhttp.FileContent)>
            <cfif VARIABLES.resultstr.status eq "OK">
                <cfset VARIABLES.drivingdetails.drivingdistance = REReplace(VARIABLES.resultstr.rows[1].elements[1].distance.text,"[^0-9.]?","","all")>
                <cfset VARIABLES.drivingdetails.drivingtime = REReplace(VARIABLES.resultstr.rows[1].elements[1].duration.text,"[^0-9.]?","","all")>
                <cfset VARIABLES.drivingdetails.drivingdistance = "-999999">
                <cfset VARIABLES.drivingdetails.drivingtime = "-999999">
                <cfset VARIABLES.drivingdetails.drivingdistance = "-999999">
                <cfset VARIABLES.drivingdetails.drivingtime = "-999999">
                <cfdump var="#cfcatch#">
        <!--- return the struct with requrired driving distance and time --->
        <cfreturn VARIABLES.drivingdetails>


<cfset source = findLatLong('300 Boylston Ave E', 'Seattle', 'WA', '98102')>
<cfset destination = findLatLong('555 N 105th St','Seattle','WA','98133')>
<cfset distance = googleGeoCodeify(source.latitude,source.longitude,destination.latitude,destination.longitude)>
<cfdump var="#distance#">

Autoversioning JS and CSS files using ColdFusion

Before going through the technique of auto-Versioning JS/CSS files, lets first understand,

What is versioning and Why versioning is required?

When we update javaScript or css files that are already cached in user’s browsers, most likely many users won’t get that for some time because of the caching at the browser or intermediate proxy(s). So we need some way to force the browser/proxy to download the latest files. So to do this either we need to change file names or URL of the files each time we change the file. As changing the file names each time after modification is not possible, so we can add a version number to the JS/CSS files to get the latest copy from the server. This technique is called as versioning.

Ex: <script type="text/javascript" src='js/global.js?v=1.2'></script>

Problem With this Approach:

The above mentioned technique works fine when we have less number of js/css files in our project. Soon it becomes very cumbersome to maintain when the project gradually becomes larger and involves a lot of js/css files for different modules.
As each time we modify some js/css files we need to update the version number in all the files where these files are included. To overcome from this problem normally 2 approaches are used by developers are:

  • 1st Approach:
    Declare a global constant called VERSION, and use this CONSTANT with all the js/css files.So with each new release we can change the verisonnumber. Ex:

    <cfset version = 1.1>
         <script type="text/javascript" src="a.js?v=#version#"></script>

    The problem with this approach is,
    Let’s say we are having 20-30 js/css files are used in our project, Suppose for the current release we have modified only 2/3 js files. Now we need to change the global version number.So by changing the global version number it will force all the js/css files to be reloaded again but will not use the cache. This creates very bad user experience for sites having huge traffic.

  • 2nd Approach:
    One better solution is to append the file last modified date time as the version number so that each time we modify a file, the user will get the latest copy of the file.This is called as auto versioning.

     Ex:<cfset  lstModified = getfileInfo("c:\test_prj\js\global.js").lastmodified >
          <script type="text/javascript" src="a.js?v=#lstModified #"></script>

It looks like a better solution, still there are some issues with this approach.
As per Google Caching Best Practices , It says not to use a query string with static file names,because it does not use cache for those files.

Don’t include a query string in the URL for static resources. Most proxies, most notably Squid up through version 3.0, do not cache resources with a “?” in their URL. To enable proxy caching for these resources, remove query strings from references to static resources, and instead encode the parameters into the file names themselves.

So what is the best solution?

So the best solution to auto version js/css/image files involves two steps.

  1. Append the last modified date time with the file name itself .
  2. Set up redirect rule on the server. The redirect rule is to redirect any files located in our \scripts\ or \css\ folders with version numbers in between the file name and the extension back to just the filename and extension. For example, I could now rewrite the url “/css/structure.css: as “/css/structure.1234.css” and Apache/IIS would see those as the exact same files. This approach is used by most of the high traffic sites like Amazon, Google etc.

So now in this way each time we modify some js/css/image file, we just do not need to change anything else on the production server, It will automatically refresh only those files that are modified and even cache the files as there is no query string appended to the file names.

AutoVersioning files using ColdFusion:
It involves two steps,

First we need to add these redirect rules.

For IIS: 
Add this to the web.config:
                <rule name="caching">
                    <match url="^(.*)([0-9]{10}\.)(css|js)$" />
                    <action type="Rewrite" url="{R:1}{R:3}" />
For Apache:
Add this to .htaccess:
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]


<cffunction name="autoVersion" access="public" output="true" returntype="string" >
    <cfargument name="fileName" type="string" required="true" >
    <cfset var absPath = expandPath("#ARGUMENTS.fileName#")>
    <cfif !fileExists(absPath)>
        <cfreturn fileName>
    <!--- get the last modified datetime of the file --->
    <cfset  lstModified = getfileInfo(absPath).lastmodified >
    <!--- get the unix timestamp --->    
    <cfset  mtime =  dateDiff("s", "January 1 1970 00:00", lstModified)>
    <cfset var fileNameWithTimestamp = listFirst(ARGUMENTS.fileName, '.') & '.' & mtime & '.' & listLast(ARGUMENTS.fileName, '.')>
    <cfreturn fileNameWithTimestamp>

            <link rel="stylesheet" href="#autoVersion('css/myStyle.css')#" />