Dumping Out The CGI Scope Does Not List All Available CGI Values In ColdFusion

I had an interesting / unfortunate journey yesterday, concerning the CGI scope. I was trying to access a value that was being passed-through by the load balancer; and, since I had never dealt with this value before, I naturally tried to CDump-out the CGI scope to see what it would be. Unfortunately, the value was not present in the CGI dump. This lead to several support tickets, which turned out to be useless since, as I am just now learning now for the first time, the CGI scope doesn't "report" all of its available keys.

Unlike a normal Struct in ColdFusion, it looks like the CGI scope will only report on a known subset of key values. You can still ask the CGI scope for which ever key you want; but, as far as CFDump and key-loops are concerned, you may only be seeing a part of the whole picture.

To demonstrate, I'm going to try to "find" the CGI value, "document_root". I will do this though both direct key reference as well as key reporting:

<cfscript>

target = ucase( "document_root" );

// Get the document root directly from the CGI scope.

writeOutput( "#target#: #cgi[ target ]#" );

writeOutput( "<br />" );

writeOutput( "- - - - - - - - - - - - - - - - -" );

writeOutput( "<br />" );

// Now, try to get the value by looping of the CGI key list

// and checking to see if the key matches our target.

for ( key in cgi ) {

writeOutput( key );

if ( key == target ) {

writeOutput( ": #cig[ key ]# <br />" );

} else {

writeOutput( ": . . . <br />" );

}

}

</cfscript>

As you can see, we try to loop over the CGI scope, looking for the "document_root" key. And, when we run the above code, we get the following output:

As you can see, while we were able to directly reference the "document_root" key, the CGI scope does not "report" it as a known value. Unfortunately, this little quirk of the language cost me a few hours of head-scratching and support tickets. Hopefully this will prevent you from doing the same!

Reader Comments

This is also the reason that the CGI doesn't throw errors if you request variables that don't actually exist. If you ask for CGI["OMGTHISISBAD"] all you get is an empty string, not some kind of undefined error.

A scope that behaves in a unique manner to other scopes. I'm find that scopes have some core differences, like whether they are persistent or not, but when I tell something to dump, you best give up the goods... and NOT be taking a cut off the top.

Gotta say, I'm not liking CGI acting in this manner. If the variables are there, show them. If you reference a variable that doesn't exist, throw an error (even to Ben's dismay) :) One of the core truths we try to hold onto when coding is that we stay consistent with our methodologies and don't invoke a haphazard logic that does Action A THIS way, but would in other instances perform Action A THAT way.

@Ben, CF9 & CF10 both support the "Elvis" syntax that you list -- I've got production code running it now. I was introduced to it by a front-end guy who asked if it was possible and I was like -- what is that?!?

Turns out it worked and I said goodbye to IIF and silly if else variable sets forever!

Absolutely. I've sent the developers of Firebug and the Web Developer Toolbar money because I freaking LOVE those tools, but it *is* still 1 more step you'd have to take to make up for an odd shortcoming with ColdFusion.

Maybe introduce a new application variable, if Adobe wants to keep it like it is so bad.

If you have those tools Installed which I think most devs do now, it's not really an extra step. And tbh I think anyone who is not a newb must be used to the deficiencies in cf by now or switched to Railo :)

We use Smart Cards here to login in and the person's info is dumped into variables in the CGI scope with the single sign on. Unfortunatley not all the variables show up in Coldfusion. I wrote an ASP page to dump all of them out and they are all there in the ASP page. However in Coldfusion, 3 of them are missing such as the person's middle name and couple of others. I don't know if this is because of tomcat or Coldfusion.

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments

Live in the Now

Oops!

Name:

Email:

( I keep this private )

Website:

Comment:

Subscribe to comments.

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please
do not post unrelated questions or
large chunks of code. And, above all, please be nice to each other - we're trying to
have a good conversation here.

I am the co-founder and lead engineer at InVision App, Inc — the world's leading prototyping,
collaboration & workflow platform. I also rock out in JavaScript and ColdFusion 24x7 and I dream about
promise resolving asynchronously.