我对ColdFusion(昨天开始)很陌生。我正在尝试在checklogin.cfm
中创建一个会话变量,该变量等于从表单输入的用户名。所以我可以在结果页面上显示用户名。有可能这样做吗?
还是有更好的方法做到这一点?
application.cfm
<cfset this.name = "Name of your application">
<cfset this.sessionManagement = true>
check_login.cfm
<!--- If username equals user1 and password equals password1, take user to home page. --->
<cfif form.userName eq "user1" AND form.password eq "password1">
<cfset session.userName = form.userName/>
<cflocation url="home.cfm" ADDTOKEN="Yes">
<!--- If username doesn't equal user1 and/or password doesn't
equal password1, take user back to login page. --->
<cfelse>
<cflocation url="login.cfm" ADDTOKEN="Yes">
</cfif>
home.cfm
<cfoutput>
<br>
<b><font size="6">Enter the numbers you want to add:</font></b>
</cfoutput>
<!--- Add numbers form --->
<cfform action="result.cfm" method="post">
<!--- Get numbers from user --->
<p>
Enter first number:
<cfinput type="Text" name="number1" required="Yes" size="20"
maxlength="100" message="You must enter a number.">
<br>
Enter second number:
<cfinput type="Text" name="number2" required="Yes" size="20"
maxlength="100" message="You must enter a second number.">
<br>
</p>
<!--- submit button --->
<cfinput type="Submit" name="submitForm" value="Submit">
</cfform>
login.cfm
<cfform action="check_login.cfm" method="post">
<!--- Get login info from user --->
<p>
UserName:
<cfinput type="Text" name="userName" required="Yes" size="20" maxlength="25"
message="Username is required and must be less than 25 characters.">
<br>
Password:
<cfinput type="password" name="password" required="Yes" size="20" maxlength="20"
message="Password is required and must be less than 20 characters.">
<br>
</p>
<!--- submit button --->
<cfinput type="Submit" name="submitForm" value="Submit">
</cfform>
result.cfm
<cfset result = form.number1 + form.number2 >
<cfoutput>
<b><font size="6">Hi #session.userName#! </font></b><br>
<br>
<!-- Display result to user. -->
<font size="5">Your result is: #result#</font>
</cfoutput>
问题出在结果页面上,其中说Element USERNAME is undefined in SESSION
指向行<b><font size="6">Hi #session.userName#! </font></b>
答案 0 :(得分:5)
要解决上述错误,您需要将会话变量的设置移至check_login.cfm文件中<cflocation>
标记上方。
从此:
<!--- If username equals user1 and password equals password1, take user to home page. --->
<cfif form.userName eq "user1" AND form.password eq "password1">
<cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. --->
<cfelse>
<cflocation url="login.cfm">
</cfif>
<cfset session.userName = form.userName/>
对于这样的事情:
<!--- If username equals user1 and password equals password1, take user to home page. --->
<cfif form.userName eq "user1" AND form.password eq "password1">
<cfset session.userName = form.userName/>
<cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. --->
<cfelse>
<cflocation url="login.cfm">
</cfif>
其原因是因为ColdFusion在遇到<cflocation>
标记时将停止处理文件的其余部分。当处理该标签时,它将立即将重定向发送回用户。因此,用于设置会话变量的代码永远不会运行。
在会话变量的设置周围使用CFLOCK的更新示例
<!--- If username equals user1 and password equals password1, take user to home page. --->
<cfif form.userName eq "user1" AND form.password eq "password1">
<cflock scope="session" type="exclusive" timeout="10">
<cfset session.userName = form.userName/>
</cflock>
<cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. --->
<cfelse>
<cflocation url="login.cfm">
</cfif>