ColdFusion CGI variables and Spiders
What's wrong with this bit of code?
1<cfset languages = CGI.HTTP_ACCEPT_LANGUAGE>
Well nothing as long as the page is viewed with a browser. However a cgi variable in your code may not exist if a spider rather than a browser looks at your site. This includes spiders like google. Your site may not be indexed by search engines if you don't handle CGI variables correctly.
So how do you check if you site works without cgi variables being set?
- Use a HTML validator. Most validators don't send the same header information as a browser would.
- Use HTTP live headers. You can record, play back and edit all header information that is sent from a browser. With HTTP live headers it's easy to make a request via a browser with no header information.
The spider safe version of the code:
1<cfset languages = "">
2<cfif structkeyexists(CGI,"HTTP_ACCEPT_LANGUAGE")>
3 <cfset languages = CGI.HTTP_ACCEPT_LANGUAGE>
4</cfif>
2<cfif structkeyexists(CGI,"HTTP_ACCEPT_LANGUAGE")>
3 <cfset languages = CGI.HTTP_ACCEPT_LANGUAGE>
4</cfif>
TweetBacks
Testing for CGI variables
Because some browsers do not support some CGI variables, ColdFusion always returns True when it tests for the existence of a CGI variable, regardless of whether the browser supports the variable. To determine if the CGI variable is available, test for an empty string, as shown in the following example:
<cfif CGI.varname IS NOT "">
CGI variable exists
<cfelse>
CGI variable does not exist
</cfif>