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>
- Open up the /www/about.cfm file in your code editor.
-
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
-
Directly after the open <cffunction> tag, create a <cfargument> tag with the following attributes:
- name: stringToBeConverted
- type: string
- required: true
-
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" />
- After the <cfargument> tag, create a <cfset> that sets an empty string to a variable called convertedString.
- 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.
-
Your code should look similar to this:
<cfset var convertedString = '' />
-
After the <cfset> tag, create a <cfloop> tag with the following attributes:
- from: 1
- to: #len(arguments.stringToBeConverted)#
- index: i
-
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)) & ';' />
- After the <cfset> tag, create a closing </cfloop> tag.
- Below the closing </cfloop> tag, create a <cfreturn> tag that returns the convertedString variable.
- Below the <cfreturn> tag, create a closing </cffunction> tag.
-
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>
</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>