The Captcha Using Coldfusion 8 is quite Easy but what I we want to make it bit diifcult for the user to read using Case Sensitiveness.
Here is how we will do it.
First of all we will list the files we need for the this Captcha Stuff. A captcha.cfm file and a Login.cfm file So here is a very easy and small code which we will make the use o the coldfusion captcha.
Captcha.cfm Code:
<cfset Str_Lower_Case_Alpha = "abcdefghijklmnopqrstuvwxyz" /> <!--- Set up available upper case values. This is done by converting the lower case string using "UCase". ---> <cfset Str_Upper_Case_Alpha = UCase( Str_Lower_Case_Alpha ) /> <!--- Set up available numbers. ---> <cfset Str_Numbers = "0123456789" /> <!--- Make a string that contains our lower case upper case and number values. ---> <cfset strAll = Str_Lower_Case_Alpha & Str_Upper_Case_Alpha & Str_Numbers /> <!--- Create the password array of 1-3 dimensions. Index array elements with square brackets: [ ]. ---> <cfset arrPassword = ArrayNew(1) /> <!--- Generate the password and put each random value in the password array string. ---> <!--- Select the random letter from our lower case set. ---> <cfset arrPassword[ 1 ] = Mid(Str_Lower_Case_Alpha,RandRange( 1, Len( Str_Lower_Case_Alpha ) ), 1 ) /> <!--- Select the random letter from our upper case set. ---> <cfset arrPassword[ 2 ] = Mid(Str_Upper_Case_Alpha, RandRange( 1, Len( Str_Upper_Case_Alpha ) ), 1 ) /> <!--- Select the random number from our number set. ---> <cfset arrPassword[ 3 ] = Mid(Str_Numbers,RandRange( 1, Len( Str_Numbers ) ),1) /> <!--- Create rest of the password. ---> <cfset arrPassword[ 4 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) /> <cfset arrPassword[ 5 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) /> <cfset arrPassword[ 6 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) /> <!--- We don?t always want the first three characters to be of the same order (by type). ---> <!--- Therefore, let's use the Java Collections utility class to shuffle this array into a "random" order. ---> <cfset CreateObject( "java", "java.util.Collections" ).Shuffle(arrPassword ) /> <!--- Join all the characters into a single string. ---> <!--- We can do this by converting the array to a list and then just providing no delimiters (empty string delimiter). ---> <cfset strPassword = ArrayToList(arrPassword, "" ) /> <!--- The password is finished. ---> <cfset rndString = #strPassword#> <cfset rndHash=Hash(#strPassword#)>
With a little bit of Modification code Provided by ScandicWeb and we will see what is going on now. Now our login.cfm Page:
<cfoutput>
<cfform method="post" action="#cgi.SCRIPT_NAME#" name="adminlogin" id="adminlogin">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td valign="center" align="center">
<table class="border" border="0" cellpadding="1" cellspacing="2" width="500">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="1" width="100%">
<caption>
Login
</caption>
<tr>
<td colspan="2" class="default" align="center">You aren't logged in or your session has expired.<br/>
Please enter your Username and Password below and click Login.</td>
</tr>
<cfif IsDefined('invalid')>
<tr>
<td align="center" colspan="2"><div class="darkred">#trim(invalid)#</div></td>
</tr>
</cfif>
<tr>
<td colspan="2" align="center"><div id='adminlogin_errorloc' class='error_strings' style='></div></td>
</tr>
<tr>
<td class="default" align="right" width="160"><strong>Username: </strong></td>
<td width="340" align="left"><cfinput type="text" name="j_username" message="Error! Please Provide username" tooltip="Provide Your Username" required="yes" class="inputstyle" id="j_username" tabindex="1" size="25" maxlength="100"></td>
</tr>
<tr>
<td class="default" align="right" width="160"><strong>Password: </strong></td>
<td width="340" align="left"><cfinput type="password" name="j_password" message="Error! Please Provide Password" tooltip="Password Required" required="yes" class="inputstyle" id="j_password" tabindex="2" size="25" maxlength="100"></td> </tr>
<tr>
<td class="default"> </td>
<td align="left">
<cfinclude template="../captcha/captcha.cfm">
<cfimage action="captcha" fontSize="25" width="200" height="50" text="#rndString#"
fonts="Arial,Verdana,Courier New" difficulty="medium" quality="1"><br>
Case Sensitive Captcha Match Required</td>
</tr>
<tr>
<td class="default" align="right"><strong>Verify Yourself:</strong>
<cfinput type="hidden" name="userInput" value="#rndHash#"></td>
<td align="left"><cfinput type="text" name="regVerifyCaptcha" message="Error! Provide Captcha Value" tooltip="Provide Exact Text as above in Image" required="yes" class="inputstyle" id="regVerifyCaptcha" tabindex="3" size="25" maxlength="10"/></td>
</tr>
<tr>
<td align="right" class="default"> </td>
<td align="left"><input type="checkbox" name="RememberMe" tabindex="4" value="Yes" <cfif IsDefined("cookie.j_username")> CHECKED</cfif>> Remember Me</td>
</tr>
<tr>
<td align="center"> </td>
<td align="left"><cfinput type="submit" name="submit" value="Login" class="tinyborder" tabindex="5">
<cfinput type="reset" name="reset" value="Clear" class="tinyborder" tabindex="6"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</cfform>
</cfoutput>
So next we will check on the page where we check that is there is a match for the captcha
<cfif Hash(Form.regVerifyCaptcha) NEQ Form.userInput> <cfset invalid = "Error! Captcha Text Mismatching, Try again"> </cfif>
Here now you have a working captcha Code with Case Sensitive Captcha Login