Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save learncfinaweek/4121237 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121237 to your computer and use it in GitHub Desktop.
Code Reuse - Hands On 12

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 = '' />
    
    &lt;cfloop from="1" to="#len(arguments.StringToBeConverted)#" index="i"&gt;
    	&lt;cfset convertedString &= '&##' & asc(mid(arguments.StringTobeConverted,i,1)) & ';' /&gt;
    &lt;/cfloop&gt;	
    
    &lt;cfreturn convertedString /&gt;
    

    </cffunction>

    	</li>
    	<li>
    		Locate the <span class="code">personalInfo.email</span> output which should be on or around line 129.
    	</li>
    	<li>
    		Wrap the <span class="code">personalInfo.email</span> output with a call to the <span class="code">convertStringToASCII</span> function call. Your code should look similar to this:
    
    #convertStringToASCII(personalInfo.email)#	
    
    	</li>
    	<li>
    		Browse to the <span class="code">/www/about.cfm</span> page in your browser.
    	</li>
    	<li>
    		You should see no change to your page.
    	</li>
    	<li>
    		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.
    	</li>
    </ol>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment