ColdFusion CGI variables and Spiders

What's wrong with this bit of code?

view plain print about
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?

  1. Use a HTML validator. Most validators don't send the same header information as a browser would.
  2. 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:

view plain print about
1<cfset languages = "">
2<cfif structkeyexists(CGI,"HTTP_ACCEPT_LANGUAGE")>
3    <cfset languages = CGI.HTTP_ACCEPT_LANGUAGE>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
From the docs

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
CGI variable does not exist
# Posted By Jay Greer | 4/18/07 12:50 AM
Thats true for certain CGI variables eg HTTP_REFERRER but not all CGI variables inc the one I mentioned above in the code.
# Posted By Justin Mclean | 4/18/07 1:16 AM