问题
Just a little clueless... using Coldfusion8, if I dump my session to file:
<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session#">
this includes:
accounttyp: whatever
I get the same result if I only dump this parameter:
<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session.accounttyp#">
Question:
If it's defined and dump-able, how come checking isDefined like so:
<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#IsDefined(Session.accounttyp)#">
turns out to be NO? If it's there it should be defined, shouldn't it?
Thanks for some clarification.
回答1:
<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#IsDefined(Session.accounttyp)#">
It is because the syntax is incorrect. IsDefined
expects the name of a variable ie a string. By omitting the quotes around the variable name, the session variable gets evaluated first, and its value ("whatever") is what gets passed into IsDefined
. So the code is actually checking for a variable named "whatever", not "session.accounttyp" ie:
<cfif IsDefined("whatever")>
That is why the result is NO
. This is the correct syntax. (Notice the quotes and lack of pound signs).
<cfif IsDefined("Session.accounttyp")>
However, I would suggest switching to structKeyExists
. It is generally preferred over IsDefined
because it is more precise.
回答2:
This returns a boolean value:
#IsDefined(Session.accounttyp)#
So, you are asking it to return yes or no.
A better test might be this:
<cfif isDefined("Session.accounttyp")>
<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session.accounttyp#">
</cfif>
来源:https://stackoverflow.com/questions/10921081/why-a-parameter-is-not-defined-if-i-can-dump-it-and-it-has-a-value-in-coldfusio