Results 1 to 3 of 3

Thread: Green Spam Prevention - cfmail form with reCaptcha

  1. #1

    Default Green Spam Prevention - cfmail form with reCaptcha

    Green Spam Prevention - cfmail form with reCaptcha
    This example makes use of the popular recaptcha. A "green" alternative to the standard cfcaptcha tag, because not only does it stop spammers from using a form for sending their emails it also takes the energy and time already being spent to translate printed material into digital material.

    "About 200 million CAPTCHAs are solved by humans around the world every day. In each case, roughly ten seconds of human time are being spent. Individually, that's not a lot of time, but in aggregate these little puzzles consume more than 150,000 hours of work each day."

    "reCAPTCHA improves the process of digitizing books by sending words that cannot be read by computers to the Web in the form of CAPTCHAs for humans to decipher. More specifically, each word that cannot be read correctly by OCR is placed on an image and used as a CAPTCHA. This is possible because most OCR programs alert you when a word cannot be read correctly.

    But if a computer can't read such a CAPTCHA, how does the system know the correct answer to the puzzle? Here's how: Each new word that cannot be read correctly by OCR is given to a user in conjunction with another word for which the answer is already known. The user is then asked to read both words. If they solve the one for which the answer is known, the system assumes their answer is correct for the new one. The system then gives the new image to a number of other people to determine, with higher confidence, whether the original answer was correct."

    Taken from "WHAT IS reCaptcha".

    ---------------------------------------------------------------------------------------

    1. Signup for an account at Recaptcha

    2. Provide a domain name where you'll be using reCaptcha.

    3. Create key, for single or all domains.

    4. Click on the "reCAPTCHA plugins and libraries", from there download the ColdFusion reCaptcha plugin or click this link.

    5. Download plugin from ria-fordge.

    6. Create and place the file "recaptcha.cfm" in your custom tag folder or in the directory where the form file exists. If you need a custom tag path created contact our fantastic support team. For this example I created the recaptcha.cfm file and placed it in the folder with the form.


    NOTE: I recommend creating a custom error page, this prevents errors from revealing your public and private key if there are any errors with the form.


    7. Create the form for capturing user data such as a contact form submission. For this tutorial I used the ColdFusion Builder and parts of examples used in the ColdFusion 8 documentation. For hosting I used the ColdFusion 9 hosting (free beta) we provide for testing ColdFusion 9 applications.

    8. Upload the form (I used the name "contact.cfm").
    Code:
    <h3>Example: Green Spam Prevention - cfmail form with reCaptcha</h3>
    
    
    <html>
    	<body>
    	<cf_recaptcha
    		privateKey="...your private key..."
    		publicKey="...your public key..."
    		action=check>
    
    <!--- Heres where the email information is sent, set the TO and FROM below --->
    	<cfif IsDefined("Form.oncethrough") AND #form.recaptcha# EQ "true">
    		<cfmail
    		to = "[email protected]"
    		from = "[email protected]"
    		subject = "Form submission!" >
    	
    		Name: #form.firstname# #form.lastname#
    		Email: #form.email#
    		DOB: #form.dob#
    		Address: #form.address#
    		City: #form.city#
    		State: #form.state#
    		Zip: #form.zip#
    		Phone: #form.phone#
    		Note: #form.note#
    		IP: #cgi.remote_addr#
    		</cfmail>
    <!--- This information is returned to the user upon submission of the form. --->
    		<cfoutput>#form.firstname#, your information was submitted successfully. We will be contacting you shortly.
    		<br>Here's what we have recorded:
    		<br>Name: #form.firstname# #form.lastname# 
        	<br>Email: #form.email# 
        	<br>Date of Birth: #form.dob# 
    		<br>Phone: #form.phone#
        	<br>Address: #form.address#
    		<br>City: #form.city# 
    		<br>State: #form.state# 
    		<br>Zip: #form.zip#
    		<br>Note: #form.note#
    		</cfoutput>
    <!--- If the user fails to validate the cfcaptcha they will be prompted to return to the contact form. Change the file name of your contact form here if different than contact.cfm --->
    	<cfelseif IsDefined("Form.oncethrough") AND #form.recaptcha# EQ "false">
    		<cfoutput>Please try again. Return to the <a href=contact.cfm>Contact Form</a></cfoutput>
    	<cfelse>
    <!--- Start collecting the contact information in the form. --->
    		<cfform>
    		<h4>Thank you for visiting our site, please fill out fully so we can contact you.</h4>
    		<cfoutput>
    		<h4>Your IP Address #cgi.remote_addr#</h4>
    		</cfoutput>
        	First Name: <cfinput type = "Text" name = "firstname" 
            	message = "Please enter your first name." 
            	validate = "required" required = "Yes">
    		<br>Last Name: <cfinput type = "Text" name = "lastname" 
            	message = "Please enter your last name." 
            	validate = "required" required = "Yes">
    		<br>Email: <cfinput type = "text" name = "email" 
            	message = "Please enter your email address." 
            	validate = "email" required = "Yes">
    		<br>Date of Birth: <cfinput type = "Text" name = "dob" 
            	message = "Please enter your date of birth." 
            	validate = "date" required = "Yes">
    		<br>Phone: <cfinput type = "Text" name = "phone"
            	validate = "telephone" required = "Yes">
    		<br><br><b>Your address is optional!</b>
    		<br>Address: <cfinput type = "Text" name = "address"
            	required = "No">
    			<br>Format: 123 Street ST, Tulsa, OK
    		<br>City: <cfinput type = "Text" name = "city"
            	validate = "zipcode" required = "No">
    		<br>State: <cfinput type = "Text" name = "state"
            	validate = "zipcode" required = "No">
    		<br>Zip: <cfinput type = "Text" name = "zip"
            	validate = "zipcode" required = "No">
    		<br><br>Have something to say? <br><cfinput type = "text" style="height: 100px;" size = "50" name = "note"
            	required = "No">
    		<br><br>Check to confirm permission to contact you: <cfinput type = "checkbox" name = "contactallowed" 
            	message = "Please confirm you permission to contact you." 
            	validate = "required" required = "Yes">
    		<cf_recaptcha
    			privateKey="...your private key..."
    			publicKey="...your public key..."
    			theme="white">
    		<p><cfinput type = "submit" name = "submit" value = "Submit">
        	<cfinput type = "hidden" name = "oncethrough" value = "Yes"></p>
    		</cfform>
    	</cfif>
    	</body>
    </html>
    This example collects the Name, Email, DOB, Address, City, State, Zip, Phone, Note, IP of the submitter, some fields are optional. I tried to cover a lot of uses, if there's something you DON'T need to collect, just remove it from between the cfform, cfmail, and cfoutput sections.

    References:
    ColdFusion MX Coding Guidelines
    Creating and calling custom tags
    cfinput
    cfform
    reCaptcha
    ColdFusion reCAPTCHA tag
    Last edited by JonC; July 30th, 2009 at 10:22 AM.
    Jon Cavanaugh
    ColdFusion Systems Analysts
    Director of Business Development
    Linux Hosting | Christian Hosting | Railo Hosting

  2. #2
    montgoma Guest

    Default Custom Error Page

    I have setup the recaptcha in my coldfusion environment successfully without any problems but I would like to know more about the custom error page. We cannot use an application.cfc in our enviroment because of the way our site was designed with fuseactions. I was wondering if there is another way to impliment it into a cfm page or otherwise.

    Quote Originally Posted by JonC View Post
    Green Spam Prevention - cfmail form with reCaptcha
    This example makes use of the popular recaptcha. A "green" alternative to the standard cfcaptcha tag, because not only does it stop spammers from using a form for sending their emails it also takes the energy and time already being spent to translate printed material into digital material.

    "About 200 million CAPTCHAs are solved by humans around the world every day. In each case, roughly ten seconds of human time are being spent. Individually, that's not a lot of time, but in aggregate these little puzzles consume more than 150,000 hours of work each day."

    "reCAPTCHA improves the process of digitizing books by sending words that cannot be read by computers to the Web in the form of CAPTCHAs for humans to decipher. More specifically, each word that cannot be read correctly by OCR is placed on an image and used as a CAPTCHA. This is possible because most OCR programs alert you when a word cannot be read correctly.

    But if a computer can't read such a CAPTCHA, how does the system know the correct answer to the puzzle? Here's how: Each new word that cannot be read correctly by OCR is given to a user in conjunction with another word for which the answer is already known. The user is then asked to read both words. If they solve the one for which the answer is known, the system assumes their answer is correct for the new one. The system then gives the new image to a number of other people to determine, with higher confidence, whether the original answer was correct."

    Taken from "WHAT IS reCaptcha".

    ---------------------------------------------------------------------------------------

    1. Signup for an account at Recaptcha

    2. Provide a domain name where you'll be using reCaptcha.

    3. Create key, for single or all domains.

    4. Click on the "reCAPTCHA plugins and libraries", from there download the ColdFusion reCaptcha plugin or click this link.

    5. Download plugin from ria-fordge.

    6. Create and place the file "recaptcha.cfm" in your custom tag folder or in the directory where the form file exists. If you need a custom tag path created contact our fantastic support team. For this example I created the recaptcha.cfm file and placed it in the folder with the form.


    NOTE: I recommend creating a custom error page, this prevents errors from revealing your public and private key if there are any errors with the form.


    7. Create the form for capturing user data such as a contact form submission. For this tutorial I used the ColdFusion Builder and parts of examples used in the ColdFusion 8 documentation. For hosting I used the ColdFusion 9 hosting (free beta) we provide for testing ColdFusion 9 applications.

    8. Upload the form (I used the name "contact.cfm").
    Code:
    <h3>Example: Green Spam Prevention - cfmail form with reCaptcha</h3>
    
    
    <html>
    	<body>
    	<cf_recaptcha
    		privateKey="...your private key..."
    		publicKey="...your public key..."
    		action=check>
    
    <!--- Heres where the email information is sent, set the TO and FROM below --->
    	<cfif IsDefined("Form.oncethrough") AND #form.recaptcha# EQ "true">
    		<cfmail
    		to = "[email protected]"
    		from = "[email protected]"
    		subject = "Form submission!" >
    	
    		Name: #form.firstname# #form.lastname#
    		Email: #form.email#
    		DOB: #form.dob#
    		Address: #form.address#
    		City: #form.city#
    		State: #form.state#
    		Zip: #form.zip#
    		Phone: #form.phone#
    		Note: #form.note#
    		IP: #cgi.remote_addr#
    		</cfmail>
    <!--- This information is returned to the user upon submission of the form. --->
    		<cfoutput>#form.firstname#, your information was submitted successfully. We will be contacting you shortly.
    		<br>Here's what we have recorded:
    		<br>Name: #form.firstname# #form.lastname# 
        	<br>Email: #form.email# 
        	<br>Date of Birth: #form.dob# 
    		<br>Phone: #form.phone#
        	<br>Address: #form.address#
    		<br>City: #form.city# 
    		<br>State: #form.state# 
    		<br>Zip: #form.zip#
    		<br>Note: #form.note#
    		</cfoutput>
    <!--- If the user fails to validate the cfcaptcha they will be prompted to return to the contact form. Change the file name of your contact form here if different than contact.cfm --->
    	<cfelseif IsDefined("Form.oncethrough") AND #form.recaptcha# EQ "false">
    		<cfoutput>Please try again. Return to the <a href=contact.cfm>Contact Form</a></cfoutput>
    	<cfelse>
    <!--- Start collecting the contact information in the form. --->
    		<cfform>
    		<h4>Thank you for visiting our site, please fill out fully so we can contact you.</h4>
    		<cfoutput>
    		<h4>Your IP Address #cgi.remote_addr#</h4>
    		</cfoutput>
        	First Name: <cfinput type = "Text" name = "firstname" 
            	message = "Please enter your first name." 
            	validate = "required" required = "Yes">
    		<br>Last Name: <cfinput type = "Text" name = "lastname" 
            	message = "Please enter your last name." 
            	validate = "required" required = "Yes">
    		<br>Email: <cfinput type = "text" name = "email" 
            	message = "Please enter your email address." 
            	validate = "email" required = "Yes">
    		<br>Date of Birth: <cfinput type = "Text" name = "dob" 
            	message = "Please enter your date of birth." 
            	validate = "date" required = "Yes">
    		<br>Phone: <cfinput type = "Text" name = "phone"
            	validate = "telephone" required = "Yes">
    		<br><br><b>Your address is optional!</b>
    		<br>Address: <cfinput type = "Text" name = "address"
            	required = "No">
    			<br>Format: 123 Street ST, Tulsa, OK
    		<br>City: <cfinput type = "Text" name = "city"
            	validate = "zipcode" required = "No">
    		<br>State: <cfinput type = "Text" name = "state"
            	validate = "zipcode" required = "No">
    		<br>Zip: <cfinput type = "Text" name = "zip"
            	validate = "zipcode" required = "No">
    		<br><br>Have something to say? <br><cfinput type = "text" style="height: 100px;" size = "50" name = "note"
            	required = "No">
    		<br><br>Check to confirm permission to contact you: <cfinput type = "checkbox" name = "contactallowed" 
            	message = "Please confirm you permission to contact you." 
            	validate = "required" required = "Yes">
    		<cf_recaptcha
    			privateKey="...your private key..."
    			publicKey="...your public key..."
    			theme="white">
    		<p><cfinput type = "submit" name = "submit" value = "Submit">
        	<cfinput type = "hidden" name = "oncethrough" value = "Yes"></p>
    		</cfform>
    	</cfif>
    	</body>
    </html>
    This example collects the Name, Email, DOB, Address, City, State, Zip, Phone, Note, IP of the submitter, some fields are optional. I tried to cover a lot of uses, if there's something you DON'T need to collect, just remove it from between the cfform, cfmail, and cfoutput sections.

    References:
    ColdFusion MX Coding Guidelines
    Creating and calling custom tags
    cfinput
    cfform
    reCaptcha
    ColdFusion reCAPTCHA tag

  3. #3
    Join Date
    Nov 2009
    Posts
    99

    Default

    Hi,

    Yes, it is possible to handle errors within Application.cfm files as well, and Ben Nadel has written some good articles on this here:

    - Handling Errors With ColdFusion CFError
    - Using ColdFusion to Handle 404 Errors

    If you are able to use an Application.cfc file, though, we recommend following this example:

    - Example for ColdFusion 8 error handling within application.cfc
    Last edited by jakeh; December 1st, 2010 at 11:48 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •