<<

Code Reuse

Hands On 12

By Simon Free (Bio)
gist

In this hands on, let's create a function that will take a string and output a string of ASCII characters. Some people think that this will stop email addresses from being spidered. This may or may not be the case, but the logic for creating a function is still the same.

Tags Used: <cfscript>, <cffunction>, <cfargument>, <cfset>, <cfloop>, <cfreturn>

Functions Used: asc, mid

  1. Open up the /www/about.cfm file in your code editor.
  2. Below the closing </cfscript> tag, create a <cffunction> tag with the following attributes:
    • name: convertStringToASCII
    • output: false
    • returntype: string
    • hint: Converts String to ASCII String
  3. Directly after the open <cffunction> tag, create a <cfargument> tag with the following attributes:
    • name: stringToBeConverted
    • type: string
    • required: true
  4. Your code should look similar to this:
    <cffunction name="convertStringToASCII" output="false" returntype="String" hint="Converts string to asccii string" >
    	<cfargument name="stringToBeConverted" type="string" required="true" />
    
    
  5. After the <cfargument> tag, create a <cfset> that sets an empty string to a variable called convertedString.
  6. Before the variable name in the <cfset> tag, enter the word Var. This will scope the variable to the scope that is local to the function and will not interfere with any other variables on the page.
  7. Your code should look similar to this:
    <cfset var convertedString = '' />	
    
  8. After the <cfset> tag, create a <cfloop> tag with the following attributes:
    • from: 1
    • to: #len(arguments.stringToBeConverted)#
    • index: i
  9. After the opening <cfloop> tag, create a <cfset>. This <cfset> tag will use string concatenation and should look similar to this:
    <cfset convertedString &= '&##' & asc(mid(arguments.StringTobeConverted,i,1)) & ';' />
    
  10. After the <cfset> tag, create a closing </cfloop> tag.
  11. Below the closing </cfloop> tag, create a <cfreturn> tag that returns the convertedString variable.
  12. Below the <cfreturn> tag, create a closing </cffunction> tag.
  13. Your function should look similar to this:
    <cffunction name="convertStringToASCII" output="false" returntype="String" hint="Converts string to asccii string" >
    	<cfargument name="stringToBeConverted" type="string" required="true" />
    	<cfset var convertedString = '' />
    	
    	<cfloop from="1" to="#len(arguments.StringToBeConverted)#" index="i">
    		<cfset convertedString &= '&##' & asc(mid(arguments.StringTobeConverted,i,1)) & ';' />
    	</cfloop>	
    	
    	<cfreturn convertedString />
    </cffunction>	
    
  14. Locate the personalInfo.email output which should be on or around line 129.
  15. Wrap the personalInfo.email output with a call to the convertStringToASCII function call. Your code should look similar to this:
    #convertStringToASCII(personalInfo.email)#	
    
  16. Browse to the /www/about.cfm page in your browser.
  17. You should see no change to your page.
  18. View the source of the page and locate where the email address is being output. You should no longer see the email address but a stream of ASCII characters.